Regular Expression help

Hi,

I have a data,
AW0001
AW0002
BW0001
BW0002
AS0001
BS0002

I want to count every data based from first two letter code,

rex.SearchPattern = “^.\b(AW|BW|AS|BS)\b.$”

The code is worked if I do type the full code like,
rex.SearchPattern = “^.\b(AW0001|BWBW0001|ASAS0001|BSBS0002)\b.$”

is there any other ways to do this with regex ? I have used .left function but way to slow.

thanks
Regards,
Arief

The first pattern won’t work because the space between a number and a letter is not considered a word break, so \\b will not match there.

I’m not sure if the items you’re interested in start each line or can be mixed into text, but assuming the latter and the digits are important, this will do it:

rex.SearchPattern = "\\b([a-z]{2})(\\d{4})"

On every match, the first subexpression will be the letters and the second will be the digits.

Not to ever want to be one to contradict the master of RegEx, but shouldn’t it be

rex.SearchPattern = "\\b([A-Z]{2})(\\d{4})"

since his example shows the two-letter codes in uppercase?

That’s fine too. By default, Xojo’s PCRE implementation is case-insensitive. If that is changed in code through the RegEx.Options or the code[/code] switch, my way would stop working if the data is indeed as shown. From that perspective, your way is probably better.

BTW, that’s why RegExRX includes the switch when copying the pattern for use in Xojo or other languages, so there is no confusion.

Thanks for the explanation. I can’t recall if I ever noticed that the implementation is case-insensitive unless explicitly changed.

Kem, I’ll love the day I will have enough money to go to XDC and meet you in person !

Yes, yes, everyone agrees, it’s the highlight of XDC.

:smiley:

(Seriously, I appreciate that.)

+1

I watch quietly from the sidelines but thanks to Kem I have become a user of regular expressions. It can be a whole lot easier than trying to program stuff. Thanks!

Hi Mr. Tekinay,

The code is working, its counting all the code read by letter from a-z.

I want to categorized it into 4 options.

rex.SearchPattern = “^.\b(AW|BW|AS|BS)\b.$”
rex.SearchPattern = “^.\b(AG|AE|AJ|BJ)\b.$”
rex.SearchPattern = “^.\b(AB|BB|AM|MW)\b.$”
rex.SearchPattern = “^.\b(AC|BC|AS|BS)\b.$”

thanks
Regards,
Arief

Its solved,
by change the option into Global, multiline and insensitive to checked value…

https://regex101.com/r/CBXWMo/1

thanks for all the helps.

Regards,
Arief