Regex help

N

niju

Hi there
I need help to build a regex expression that matches
any alphanumeric word that has at least one number.

Any help would be much appreciated

Niju
 
J

John Rivers

some ideas to help you: (in t-sql format, sorry about that)

first check that it is alphanumeric
(ie: doesn't include anything out of that range)

like "%[^a-z0-9]%" (true = fail)

then check that at least one digit is present:

like "%[0-9]%" (true = succeed)

put it all together:

not var like "%[^a-z0-9]%" and var like "%[0-9]%"

only returns true for alphanumerics with at least one digit

now i will go and learn .net regex
 
T

tom pester

"any alphanumeric word that has at least one number" gives

(?=\w*\d)\w*

And to try it :

Try
Dim RegexObj As New Regex("(?=\w*\d)\w*")
Dim MatchResults As Match = RegexObj.Match(SubjectString)
While MatchResults.Success

MatchResults = MatchResults.NextMatch()
End While
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try

It uses a technique called lookahead. It matches also single digital numbers.
If you dont want that :

(?=\w*\d)(?=\w*[a-z])\w*

Let me know if you have any more questions...

Cheers,
Tom Pester
 
J

John Rivers

thanks tom, you brought assertions in regular expressions alive for me
:)
before i read your post i didn't have a clue what they were!



tom said:
"any alphanumeric word that has at least one number" gives

(?=\w*\d)\w*

And to try it :

Try
Dim RegexObj As New Regex("(?=\w*\d)\w*")
Dim MatchResults As Match = RegexObj.Match(SubjectString)
While MatchResults.Success

MatchResults = MatchResults.NextMatch()
End While
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try

It uses a technique called lookahead. It matches also single digital numbers.
If you dont want that :

(?=\w*\d)(?=\w*[a-z])\w*

Let me know if you have any more questions...

Cheers,
Tom Pester
Hi there
I need help to build a regex expression that matches
any alphanumeric word that has at least one number.
Any help would be much appreciated

Niju
 
T

tom pester

For more info on Zero-Width Assertions aka lookaround give this a try :

http://www.regular-expressions.info/lookaround.html

Cheers,
Tom Pester
thanks tom, you brought assertions in regular expressions alive for me
:)
before i read your post i didn't have a clue what they were!
tom said:
"any alphanumeric word that has at least one number" gives

(?=\w*\d)\w*

And to try it :

Try
Dim RegexObj As New Regex("(?=\w*\d)\w*")
Dim MatchResults As Match = RegexObj.Match(SubjectString)
While MatchResults.Success
MatchResults = MatchResults.NextMatch()
End While
Catch ex As ArgumentException
'Syntax error in the regular expression
End Try
It uses a technique called lookahead. It matches also single digital
numbers. If you dont want that :

(?=\w*\d)(?=\w*[a-z])\w*

Let me know if you have any more questions...

Cheers,
Tom Pester
Hi there
I need help to build a regex expression that matches
any alphanumeric word that has at least one number.
Any help would be much appreciated
Niju
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top