Pattern for regex that matches one digit at least

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Pattern for regex that matches one digit at least

Post by YasserKhalil »

Hello everyone
I am searching for a way to type a pattern that matches 9 length of letters and numbers ...but at least must have a digit at least
I have used this

Code: Select all

[a-zA-Z0-9]{9}
But if the word is of 9 letters only it matches and I don't want to match that because there is no digits at all in this case
so
123456789 is acceptable
and 125AYH6AX is acceptable
but AbcDEFghI is not acceptable

The thread is also posted at this link
https://www.excelforum.com/excel-progra ... least.html" onclick="window.open(this.href);return false;

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pattern for regex that matches one digit at least

Post by HansV »

I see that you already have a solution.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Pattern for regex that matches one digit at least

Post by YasserKhalil »

It is semi-solution
Have a look at post #9 in the other link to see what I mean
Thanks a lot Mr. Hans

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pattern for regex that matches one digit at least

Post by HansV »

I can't view the code on ExcelForum, sorry.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pattern for regex that matches one digit at least

Post by HansV »

Here is a simplistic solution using 2 patterns:

Code: Select all

Function TestString(s As String) As Boolean
    Dim re As Object
    Dim f1 As Boolean
    Dim f2 As Boolean
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "^[A-Za-z0-9]{9}$"
    f1 = re.Test(s)
    re.Pattern = "[0-9]"
    f2 = re.Test(s)
    TestString = f1 And f2
End Function
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Pattern for regex that matches one digit at least

Post by YasserKhalil »

Thanks a lot Mr. Hans
In fact I am not searching for a code. I am searching for a pattern
Please have a look at this link to see what I mean
https://regex101.com/r/3Rgp9Q/1" onclick="window.open(this.href);return false;

In the link there are the matches results and it is ok but for two instances 'ABCDEBATX' and 'AAAAAAAAA'
I know you resolve it in the UDF with two steps .. but is it possible to have one pattern for that

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Pattern for regex that matches one digit at least

Post by HansV »

It can be done with lookaheads but I can't help you with that.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Pattern for regex that matches one digit at least

Post by YasserKhalil »

Never mind at all Mr. Hans
Thanks a lot for help

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Pattern for regex that matches one digit at least

Post by rory »

Just to repeat, the pattern I gave you at EF works on the link you provided. It shows no matches for either of those entries.
Regards,
Rory

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Pattern for regex that matches one digit at least

Post by YasserKhalil »

Thanks a lot
It should match the words that have length 9 and have letters and numbers or numbers only but not all letters .. so I explained that there are two matched that I want to exclude...

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: Pattern for regex that matches one digit at least

Post by rory »

Again, the pattern I posted does not accept either of those entries as valid.
Regards,
Rory

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Pattern for regex that matches one digit at least

Post by YasserKhalil »

It was solved at this link
https://www.excelforum.com/excel-progra ... ost4952395" onclick="window.open(this.href);return false;

Thanks a lot for all great help masters