Leo1906

Need help with RegExp

6 posts in this topic

Hello Guys,

I need a little help with an regular expression.
I have this String, for example this one: BNPA
and I want to check if the string only contains the letters A-G and M-Q. And I want every character to get checked, not only the first one. I came up with this one:

StringRegExp("BNPA", '([A-G][M-Q]){4}'))

But it returns 0. Without using the {4} it should generally be wrong, right? Because then it seems like it only checks the first character. Changing the second or third character to "Z" will still return true if I don't use the {4}.

Can anybody help me with that? :)

Thanks a lot :)

Share this post


Link to post
Share on other sites



You might use an alternation

$res = StringRegExp("BNPA", '([A-G]|[M-Q]){4}') 
Msgbox(0,"", $res)
$res = StringRegExp("BNZA", '([A-G]|[M-Q]){4}') 
Msgbox(0,"", $res)

 

1 person likes this

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

20 minutes ago, mikell said:

You might use an alternation

$res = StringRegExp("BNPA", '([A-G]|[M-Q]){4}') 
Msgbox(0,"", $res)
$res = StringRegExp("BNZA", '([A-G]|[M-Q]){4}') 
Msgbox(0,"", $res)

 

Thanks a lot! That really does the job :)

But here's another one:

$res = StringRegExp("OX", '([A-C]|[L-Z]{2}')
Msgbox(0,"", $res)

I just tried to adapt what worked before ..
What am I missing this time? :/

So this time the String could be A-C AND/OR L-Z and always consist of two letters ..

Edited by Leo1906

Share this post


Link to post
Share on other sites
14 minutes ago, Leo1906 said:

What am I missing this time?

You missed the typo    :)   '([A-C]|[L-Z]){2}'

Share this post


Link to post
Share on other sites

Haha damn. Sometimes things are easy but hard to see :D

Thanks for your help! :)

Share this post


Link to post
Share on other sites

Glad I could help  :)

Notes :  you also could use a unique class, and for more precision you should use the start-of-string and end-of-string anchors  ^ and $

$res = StringRegExp("OX", '^([A-CL-Z]){2}$')
Msgbox(0,"", $res)

$res = StringRegExp("OXB", '^([A-CL-Z]){2}$')
Msgbox(0,"", $res)

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now