Leo1906 Posted January 19, 2017 Share Posted January 19, 2017 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 Link to comment Share on other sites More sharing options...
mikell Posted January 19, 2017 Share Posted January 19, 2017 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) Leo1906 1 Link to comment Share on other sites More sharing options...
Leo1906 Posted January 19, 2017 Author Share Posted January 19, 2017 (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 January 19, 2017 by Leo1906 Link to comment Share on other sites More sharing options...
mikell Posted January 19, 2017 Share Posted January 19, 2017 14 minutes ago, Leo1906 said: What am I missing this time? You missed the typo '([A-C]|[L-Z]){2}' Link to comment Share on other sites More sharing options...
Leo1906 Posted January 19, 2017 Author Share Posted January 19, 2017 Haha damn. Sometimes things are easy but hard to see Thanks for your help! Link to comment Share on other sites More sharing options...
mikell Posted January 19, 2017 Share Posted January 19, 2017 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) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now