Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Find Replace Regular Expression(find/replace is not working as expected)
Find Replace Regular Expression [message #1007638] Wed, 06 February 2013 20:35 Go to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 4
Registered: February 2013
Junior Member
Hello all,

I have an odd problem with regular expressions in the find replace feature of Eclipse. I'm using Juno on Mac OS X 10.7. I am using Classic Eclipse with PDT and Subversive plugins installed.

I have the following options checked:
Direction: Forward
Scope: All
Options: Case_Sensitive, Regular Expressions

Give the following expression in the "Find" field:
\,\s\"

I can replace it with the given expression, essentially adding a new line:
, \r\n\"

However, if I get a little more complex with my expression, the replace function no longer works, even though my expression is matched with better precision:
Find field:
(\,\s\")(?=[A-Z]{2})

Replace With Field is the same as above.

Does anyone have any idea why the lookahead syntax seems to trump the replace functionality of the find/replace features?
Re: Find Replace Regular Expression [message #1007964 is a reply to message #1007638] Thu, 07 February 2013 15:14 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

If I'm reading this correctly (regexp always get messy :-))
> Find field:
> (\,\s\")(?=[A-Z]{2})
>

says to find a ',', whitespace, or double-quote followed by exactly two
alphabetic characters, not three alphas or one alpha but two. Anything
else and it won't match. Then only if it matches will it replace the
',', whitespace, or double-quote while leaving the two alphas there.

Is that what you are actually searching for?

Thanks,
Rich
Re: Find Replace Regular Expression [message #1008043 is a reply to message #1007964] Fri, 08 February 2013 16:01 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 4
Registered: February 2013
Junior Member
What I'm searching for is:

, "AA

I'm adding a new line between the comma and double quote but only if the two characters after double quote are two upper case characters

IE

{"AA": "some arbitrary data", "name": "some arbitrary data"}, "BB": "other arbitrary data", "name": "more data"}

becomes:

{"AA": "arbitrary data", "name": "arbitrary data",
"BB": "other arbitrary data", "name": "more data"}

The two characters are ISO 3166-a alpha-2 characters (2 character country codes), but I don't want to split on the ISO codes and the "name" field. I want to split on the ISO codes only.

I have some Geospatial data in this format but it's all on one line and the ISO code data is an SVG path to be drawn. The file is 8MB and is a pain to work with as one line so I wanted to split it up to the ~250 lines.

I ended up writing a python script that opened the file, read it all, did the split using the same find replace syntax and wrote the file, but I think Eclipse should have been able to do this.

This made things a lot easier to work with. Instead of eclipse taking 30 seconds to type one character, I was able to edit it like a normal file is expected to behave and not like I was trying to edit an 8MB file on a 386 with only 8MB of memory.



Re: Find Replace Regular Expression [message #1008064 is a reply to message #1008043] Fri, 08 February 2013 17:07 Go to previous messageGo to next message
Eclipse UserFriend
hi,

Yeah that seemed to confuse it, so I changed it to:

Find: (\,\s\")([A-Z]{2})
Replace: , \r\n\"(1)
CaseSensitive: true (requires case-sensitive. If not then [A-Z] still
matches "bb" in addition to "BB").

Rich
Re: Find Replace Regular Expression [message #1008065 is a reply to message #1008043] Fri, 08 February 2013 17:11 Go to previous messageGo to next message
Eclipse UserFriend
Correction:
hi,

Yeah that seemed to confuse it, so I changed it to:

Find: (\,\s\")([A-Z]{2})
Replace: , \r\n\"$2
CaseSensitive: true (requires case-sensitive. If not then [A-Z] still
matches "bb" in addition to "BB").

Rich
Re: Find Replace Regular Expression [message #1008288 is a reply to message #1008065] Mon, 11 February 2013 19:10 Go to previous messageGo to next message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 4
Registered: February 2013
Junior Member
Thanks, I will keep that in mind the next time.

I wrote a python script, but I figured that my original look ahead query should work.

I wasn't aware I could use variables to replace partial matched strings.
(/*regex part 1 = $1*/)(/*regex part 2 = $2*/)(/*regex part 3 = $3*/)......(/*regex part N = $N*/)(/*regex part N+1 = $N+1*/)
Each set of parenthesis will increment the variable name used to substitute a partial string match with itself, Correct?
Re: Find Replace Regular Expression [message #1008306 is a reply to message #1008288] Mon, 11 February 2013 20:49 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

> Each set of parenthesis will increment the variable name used to
> substitute a partial string match with itself, Correct?

That's pretty much true, but you need to read the javadoc on regular
expression groups. They nest i.e. (a(b)c)(d) so $1 is a(b)c and $2 is b
and $3 is d.

Rich
Re: Find Replace Regular Expression [message #1008420 is a reply to message #1008306] Tue, 12 February 2013 12:28 Go to previous message
Missing name Mising name is currently offline Missing name Mising nameFriend
Messages: 4
Registered: February 2013
Junior Member
So the variables scope is at the top levels, which makes sense, given that the regular expressions are confused by look aheads.

Thanks so much
Previous Topic:Setting the image of a viewreference programmatically
Next Topic:IResourceDeltaVistor and handling a project copy
Goto Forum:
  


Current Time: Wed Apr 24 20:20:27 GMT 2024

Powered by FUDForum. Page generated in 0.03666 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top