I'm having problems handling regular expressions in Java. I don't have experience with that and I'm reaching a deadline. The problem is I'm using a tool to generate INSERT scripts, but it's broken for some reason, and I have to fix the generated .sql file on my side. Simply, I have to replace all '[' for '', all ']' for '' and finally all 'N'' for '''.
I know I could use String.replace() method for '[' and ']', but I need an regular expression for "N'" using String.replaceAll(). I already tried "[N][']" and "N'", but doesn't work. Can anybody help me?
I think we need more info. I see that you want to drop the N in
but apparently you don't want to drop it in
So I think we may need some more examples of cases where N appears which you want to drop.
As a guess though, if the cases where you want to drop N are always of the form
then you might use
where \\1 is a reference to what was found in group 1, which is whatever was inside the first (and only) set of parentheses in the replacement pattern. In other words, it gives you whatever was represented by '\\w*+' in the regex.
"I'm not back." - Bill Harding, Twister
Joined: May 16, 2003
Thanks for replying Jim, and yes, I just wanna get rid of "N" while finding something like N'EAD'.
Well, I tried what you sugested, but it didn't work yet. I realised it works when I do something like:
, you cannot have a quantifier '+' of a quantifier '*'. Also you could make it more efficient by not doing a capture but doing a "look ahead":
[ July 08, 2005: Message edited by: Kevin Davies ] [ July 08, 2005: Message edited by: Kevin Davies ]
Joined: May 06, 2004
And, since the replacement is now the empty string, you can get rid of the square brackets at the same time:BTW, "\\w*+" is a valid regex. The plus sign makes the asterisk possessive; i.e., it will match zero or more word characters and never back off, even if that makes the overall match fail. In this case, it doesn't change what gets matched, but it makes the regex (negligibly) more efficient. Possessive quantifiers are mainly useful for making sure that matches that are going to fail, do so as quickly as possible. The JDK regex implementation was the first to implement them, but others are starting to follow suit. [ July 09, 2005: Message edited by: Alan Moore ]