ale1981 Posted October 27, 2014 Share Posted October 27, 2014 (edited) Can somebody help me with this RegEx expression? I have tried but can't seem to get it to work, what I need is to verify a string, the string can only contain these characters ( C, X, J, P, L, B ) and numbers but must also be in this format; [char][number][space][char][number]...... e.g. C1 X4 B1 C3 Is this something RegEx can do? Thanks in advance. Edited October 27, 2014 by ale1981 Link to comment Share on other sites More sharing options...
jguinch Posted October 27, 2014 Share Posted October 27, 2014 (edited) $string = "C1 X4 B1" ; $string = "C1 X4 B1 X9 J0" If StringRegExp($string, "^([BCJLPX]\d)( [BCJLPX]\d)*$") Then ConsoleWrite("ok") Match C1 X4 B1 C1 X4 B1 X9 J0 Edited October 27, 2014 by jguinch Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted October 27, 2014 Share Posted October 27, 2014 (edited) $string = "C3" ;$string = "C1 X4 B1 X9 J0" ;$string = "C1a" If StringRegExp($string, "^([BCJLPX]\d(?=\h|$))+") Then ConsoleWrite("ok") Strictly following the requirements Edited October 27, 2014 by mikell Link to comment Share on other sites More sharing options...
kylomas Posted October 27, 2014 Share Posted October 27, 2014 @mikell - The h is for the space? Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jguinch Posted October 27, 2014 Share Posted October 27, 2014 @mikell : $string = "C1 CC CC CC" returns OK with your code. BTW, I did not use h but a space instead (because TAB should not match) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
kylomas Posted October 27, 2014 Share Posted October 27, 2014 Why doesn't this work... If StringRegExp($string, "^([BCJLPX]\d)( \1)*$") Then ConsoleWrite("ok") "1" referring back to the first captured group. Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Solution jguinch Posted October 27, 2014 Solution Share Posted October 27, 2014 (edited) @kylomas : your expression matches something like C1 C1 C1 C1 only (where 1 is the group value, not the expression). Use (?1) instead if 1 : If StringRegExp($string, "^([BCJLPX]\d)( (?1))*$") Then ConsoleWrite("ok") Edited October 27, 2014 by jguinch ale1981 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted October 27, 2014 Share Posted October 27, 2014 (edited) kylomas, h is for any horizontal white space, in this case "space" is not well defined so let's assume that it may be a chr(32) as well as a horizontal tab @jguinch Thus "TAB should not match" is not sure If not StringRegExpReplace($string, "[BCJLPX]\d(?:\h+|$)", "") Then ConsoleWrite("ok") Edited October 27, 2014 by mikell Link to comment Share on other sites More sharing options...
ale1981 Posted October 27, 2014 Author Share Posted October 27, 2014 @kylomas : your expression matches something like C1 C1 C1 C1 only (where 1 is the group value, not the expression). Use (?1) instead if 1 : If StringRegExp($string, "^([BCJLPX]\d)( (?1))*$") Then ConsoleWrite("ok") Thanks jguinch that worked Link to comment Share on other sites More sharing options...
kylomas Posted October 27, 2014 Share Posted October 27, 2014 (edited) @jguinch - Thanks, I was telling it to match the value captured when I meant to repeat the expression, DOH! edit: @mikell - Thanks... Edited October 27, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill 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