Regular Expression Help

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

Post Reply
neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Regular Expression Help

Post by neofax »

I am trying to make a regular expression to go thru code and beautify it. My problem is I cannot get it to be a one stop replace all expression as it is either too greedy or not greedy enough. What I want is a expression that looks for a single "=" sign that is surrounded by no spaces and the beginning of the line does not begin with <!--. Then for the replace line, I want to add spaces around the "=". So, it cannot include <, >, =(second instance) or !. Basically only alphanumeric and case insensitive.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regular Expression Help

Post by aliasmask »

Whenever you specify things around your target that vary you can't do it with only a replace statement. You'll have to use the Wiki: strfind() in combination with Wiki: substring() to rebuild the string. That's because the elements you specify will be replaced too. If they didn't vary, then you can specify the surrounding parts. I suppose you could make a replace for each condition. I'm still a newb using strfind but I've had some success. I think using Wiki: getFindCount() will return your successes along with Wiki: getGroupStart() and Wiki: getGroupEnd() but I can't be specific with the how without experimenting and doing it myself.

I think this may work for most of what you said: "[^=><!- ](=)" and "(=)[^- ]"

Basically repalce the "=" with " =" for the first pass and "=" with "= " for the second pass. You can try and do it all at once, but I think you'll miss statements like "if(a= b)". You may run in to trouble with your loop has more than 1000 occurrences.

I need to wingrep my code to find where I used it (if I actually used it) to show what I did. I forget what I did and where at the moment.
Last edited by aliasmask on Mon Jul 19, 2010 6:25 pm, edited 1 time in total.

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Regular Expression Help

Post by JamzTheMan »

You can start with something like: (?<=[a-zA-Z0-9])=(?=[a-zA-Z0-9])

Not sure off hand how to add the test in for skipping comments...

Test Samples used:
if(x=y)
if(19=19)
if(20!=19)
if(x==y)
if(x>=y)
if9x<=19)
<!-- this is an = test for x=y -->
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regular Expression Help

Post by aliasmask »

JamzTheMan wrote:You can start with something like: (?<=[a-zA-Z0-9])=(?=[a-zA-Z0-9])

Not sure off hand how to add the test in for skipping comments...

Test Samples used:
if(x=y)
if(19=19)
if(20!=19)
if(x==y)
if(x>=y)
if9x<=19)
<!-- this is an = test for x=y -->
Is that for replace? Are you assigning ? the value for a group and use \1 \2 in the replace? Can you post your code please. I tried using groups with a replace, even posted my question but didn't get any replies to solve the problem so I figured it wasn't possible with Wiki: replace().

edit:
I think for the comments, he doesn't want to replace <!---============---> with a bunch of spaces in between the "=".

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Regular Expression Help

Post by JamzTheMan »

The regular expression I posted (?<=[a-zA-Z0-9])=(?=[a-zA-Z0-9]) simply does pre and post look ups. ie it ONLY finds an = sign if it has a letter or number immediately before and after it, and it ONLY matchs the = sign in the group (it does not keep what is before or after the = sign)

So it won't match == or != or ======= etc.
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regular Expression Help

Post by aliasmask »

Cool. I've been looking for this functionality. I assume ?<= means before and ?= means after.

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Regular Expression Help

Post by neofax »

I am not trying to use the regular expression in MapTools. I am trying to use it in Notepad++. I am working through someone else's code and standardizing it so I can read it. As it is 7500+ lines of code, fixing by hand each line is a PITA. JAMZ thanks for the expression, but as Aliasmask said how do I keep the text that comes before and after? With \1 and \2?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Regular Expression Help

Post by aliasmask »

Oh, in Notepad... Yes, use the group number to replace it. I just tested it and it works.

Code: Select all

Find: ([a-zA-z0-9])=([a-zA-z0-9])
Replace: \1 = \2
But that won't do unbalanced text like "a= b". So you could break it up in to 2 replaces:

Code: Select all

Find: ([a-zA-z0-9])=
Replace: \1 =

Find: =([a-zA-z0-9])
Replace: = \1

neofax
Great Wyrm
Posts: 1694
Joined: Tue May 26, 2009 8:51 pm
Location: Philadelphia, PA
Contact:

Re: Regular Expression Help

Post by neofax »

Thanks! This worked perfectly! Now to figure out to properly nest CODE: blocks.

User avatar
JamzTheMan
Great Wyrm
Posts: 1872
Joined: Mon May 10, 2010 12:59 pm
Location: Chicagoland
Contact:

Re: Regular Expression Help

Post by JamzTheMan »

aliasmask wrote:Cool. I've been looking for this functionality. I assume ?<= means before and ?= means after.
Correct. And adding a ! makes it a negative lookup. Go to http://gskinner.com/RegExr/ and you will find these and more on the right side of the page. Plus you can test them all :)

Although Notepad++ apparently doesn't like those expressions and must use a subset of regexp, it is a valid expression in java/mt.
-Jamz
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork

User avatar
mfrizzell
Dragon
Posts: 762
Joined: Sat Feb 13, 2010 2:35 am
Location: California

Re: Regular Expression Help

Post by mfrizzell »

Jamz,
I've been to the site you mention and unless I'm doing something terribly wrong expressions I test and get to work on that site don't then work in Maptools. Is it a flavor thing? I've noticed for instance that I can test something with a single \ on the website but Maptools requires two or even three \ to do the same thing.
The above is paraphrasing to a dcegree because it's been awhile since I did it and I was trying to learn it when I was.
Any quick insights? Rule of thumb? Some nugget that will make my life easier?
Thanks.
DCI/RPGA# 7208328396 Skype ID mfrizzell77
Characters:
Strabor - Dwarf Avenger 5th Level
Tikkanan - Human Warlock 2nd Level
----------------------------------------------------
"People are more violently opposed to fur than leather because it's safer to harass rich women than motorcycle gangs."

User avatar
biodude
Dragon
Posts: 444
Joined: Sun Jun 15, 2008 2:40 pm
Location: Montréal, QC

Re: Regular Expression Help

Post by biodude »

MapTools uses the "Java" flavour of regular expressions, which adds extra backslashes (presumably to escape the backslash needed in the actual regular expression pattern). See this site for more info.
Rumble recommends using the above-mentioned site to test patterns, then you can go over to:
http://www.cis.upenn.edu/~matuszek/Gene ... ester.html
to convert them to Java versions.
"The trouble with communicating is believing you have achieved it"
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]

Post Reply

Return to “General Discussion”