Regular Expression Help
Moderators: dorpond, trevor, Azhrei
Regular Expression Help
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
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.
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.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
- JamzTheMan
- Great Wyrm
- Posts: 1872
- Joined: Mon May 10, 2010 12:59 pm
- Location: Chicagoland
- Contact:
Re: Regular Expression Help
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 -->
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
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork
Re: Regular Expression Help
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().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 -->
edit:
I think for the comments, he doesn't want to replace <!---============---> with a bunch of spaces in between the "=".
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
- JamzTheMan
- Great Wyrm
- Posts: 1872
- Joined: Mon May 10, 2010 12:59 pm
- Location: Chicagoland
- Contact:
Re: Regular Expression Help
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.
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
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork
Re: Regular Expression Help
Cool. I've been looking for this functionality. I assume ?<= means before and ?= means after.
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Regular Expression Help
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
Oh, in Notepad... Yes, use the group number to replace it. I just tested it and it works.
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])=([a-zA-z0-9])
Replace: \1 = \2
Code: Select all
Find: ([a-zA-z0-9])=
Replace: \1 =
Find: =([a-zA-z0-9])
Replace: = \1
Downloads:
- Notepad++ MapTool addon
- RPEdit details (v1.3)
- Coding Tips: Modularity and Design
- Videos: Macro Writing Tools
Re: Regular Expression Help
Thanks! This worked perfectly! Now to figure out to properly nest CODE: blocks.
- JamzTheMan
- Great Wyrm
- Posts: 1872
- Joined: Mon May 10, 2010 12:59 pm
- Location: Chicagoland
- Contact:
Re: Regular Expression Help
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 allaliasmask wrote:Cool. I've been looking for this functionality. I assume ?<= means before and ?= means after.
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
____________________
Custom MapTool 1.4.x.x Fork: maptool.nerps.net
Custom TokenTool 2.0 Fork: tokentool.nerps.net
More information here: MapTool Nerps! Fork
Re: Regular Expression Help
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.
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."
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."
Re: Regular Expression Help
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.
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 ]
[ d20 StatBlock Importer ] [ Batch Edit Macros ] [ Canned Speech UI ] [ Lib: Math ]