Page 1 of 1

Regular Expression Help

Posted: Mon Jul 19, 2010 3:27 pm
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.

Re: Regular Expression Help

Posted: Mon Jul 19, 2010 6:16 pm
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 [wfunc]strfind[/wfunc] in combination with [wfunc]substring[/wfunc] 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 [wfunc]getFindCount[/wfunc] will return your successes along with [wfunc]getGroupStart[/wfunc] and [wfunc]getGroupEnd[/wfunc] 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.

Re: Regular Expression Help

Posted: Mon Jul 19, 2010 6:24 pm
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 -->

Re: Regular Expression Help

Posted: Mon Jul 19, 2010 6:29 pm
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 [wfunc]replace[/wfunc].

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

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 1:09 pm
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.

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 1:27 pm
by aliasmask
Cool. I've been looking for this functionality. I assume ?<= means before and ?= means after.

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 1:59 pm
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?

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 2:23 pm
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

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 3:29 pm
by neofax
Thanks! This worked perfectly! Now to figure out to properly nest CODE: blocks.

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 6:05 pm
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.

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 6:29 pm
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.

Re: Regular Expression Help

Posted: Tue Jul 20, 2010 9:22 pm
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.