How to use RegEx to count case sensitive characters

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
bnc1
Pro Scripter
Posts: 127
Joined: Sun Jul 31, 2005 5:10 pm

How to use RegEx to count case sensitive characters

Post by bnc1 » Sat Jul 25, 2009 12:08 pm

I am using Regex to do a simple count of certain characters in a string

Code: Select all

let>fen={"8/6pk/p2q3p/1p5P/2pbQ3/P1N5/1P3PP1/6K1"}
let>pattern=[<pnbrqk>]
regex>pattern,fen,1,matches,num,0
mdl>num
This code works fine and yields the expected result of 16. Now I would like to count only specific upper or lower case characters :?: . For example if I could count just the upper case occurrences of PNBRQK I would get a result of 8.

I have looked at this forum thread http://www.mjtnet.com/forum/viewtopic.p ... pper+lower and have also looked at the website for EasyPatterns at http://www.datamystic.com/easypatterns_reference.html but I am not making any progress. I am fairly new to RegEx and any help would be appreciated :)

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Jul 25, 2009 3:26 pm

The default mode for this flavor of RegEx is ignore Case.
(?-i) is a flag to remove Ignore Case, make it Case Sensitive.
Put this modifier at the beginning of the Match Pattern (Needle)

Count UpperCase chars

Code: Select all

let>fen={"8/6pk/p2q3p/1p5P/2pbQ3/P1N5/1P3PP1/6K1"}
let>pattern=(?-i)[PNBRQK]
regex>%pattern%,%fen%,1,matches,num,0,,
mdl>num
Count LowerCase chars

Code: Select all

let>fen={"8/6pk/p2q3p/1p5P/2pbQ3/P1N5/1P3PP1/6K1"}
let>pattern=(?-i)[pnbrqk]
regex>%pattern%,%fen%,1,matches,num,0,,
mdl>num
Count all chars, ignore Case

Code: Select all

let>fen={"8/6pk/p2q3p/1p5P/2pbQ3/P1N5/1P3PP1/6K1"}
let>pattern=[PNBRQK]
regex>%pattern%,%fen%,1,matches,num,0,,
mdl>num
Thanks to Marcus who provided that info in an earlier post a few weeks ago.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

bnc1
Pro Scripter
Posts: 127
Joined: Sun Jul 31, 2005 5:10 pm

Post by bnc1 » Sat Jul 25, 2009 6:01 pm

Thank you for the help :!:

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Wed Jul 29, 2009 5:16 am

Hi Bob,

Would your examples read exactly the same if we did not use EasyPatterns (except for the 0 instead of the 1 for the EasyPatterns switch)?
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Wed Jul 29, 2009 6:56 am

Thanks for making me laugh.......

1. The answer is YES. I just tested the examples above and changed the Easy Pattern flag from 1 to 0.

2. I had to laugh, because I never noticed the flag value. I did not realize that Easy Patterns was set active.

I never use Easy Patterns, and have not tried to learn them yet. When I made the examples, I just copied the original sample and modified the search pattern by adding the (?-i) modifier to the front. Easy Patterns never crossed my mind. I will have to be more alert in the future.

Easy Patterns may be easier for those who are new to RegEx, I can't comment on that. Because I have worked with other "flavors" of RegEx in other programs I found it easier to keep working with what I knew. And one more note from the HELP on RegEx. Macro Scheduler's "flavor" is based on the PERL version. But many programmers may be more fmiliar with the versions from C#, Java, Javsscript, PHP, Python, Ruby, VB.Net, TextPad, and others.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Wed Jul 29, 2009 9:02 pm

Hi Bob,

Glad you enjoyed that. I've dabbled with EasyPatterns and have found I can do some things with them that I can't (or don't know how to) with a normal regex... or it would otherwise take more lines of code, for example:
  • [mustBeginWith(...) ...], [mustNotBeginWith(...) ...]
    When a match is found, it must be/must not be preceded by what is in the brackets. The bracket contents are NOT included in the actual match. The bracket contents are limited to fixed length strings - so no '3+' etc are allowed. This must be the first part of your pattern.

    [mustBeginWith( 'hello' or 'goodbye' ) 'fred']
That makes it easy to parse text, find but throw away a label and capture just the text beside the label in the first match... using just one RegEx> command.

If anyone knows how to do this with a one-line regex command without using EasyPatterns, please post an example... always good to learn new tricks.

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Sat Aug 22, 2009 5:48 am

Hello jpuziano.....

I missed your request a few weeks ago, I just ran across this tonight...
Hope its not too late for you.

You asked for: [mustBeginWith( 'hello' or 'goodbye' ) 'fred']

This works for me:

Code: Select all

Let>vSample=hello fred,hi fred,goodbye fred,bye fred
Let>vHaystack=%vSample%
Let>vNeedle=(hello|goodbye) fred
RegEx>%vNeedle%,%vHaystack%,0,vMatch,vMatchNumber,0,,

Let>vCount=0
Let>vList=

Label>Loop
Let>vCount=%vCount%+1
Let>vItem=vMatch_%vCount%
Let>vList=%vList%%CRLF%%vItem%
If>%vCount%=%vMatchNumber%,End
Goto>Loop

Label>End
MessageModal>Found %vMatchNumber% matches: %CRLF%%vList%
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts