Steve
Steve
Originally posted by Phil Powell:
Thanks, it works now but very poorly.
Your suggestion about the backslashes only partially works in Java. It works only on the very last instance of the pattern found, even if I use replaceAll(). Put it in a continuous loop and it will clean up every instance, backwards, from the last to the first.
Originally posted by Mark Vedder:
{Note: I was typing up my reply as Steve posted his and therefore did not see it until I was done. So there's a little bit of redundancy. Great minds think alike and all that...}
That has nothing to do with using an escaping a character in the replacement string. ...
[ August 25, 2008: Message edited by: Mark Vedder ]
Originally posted by Mark Vedder:
{Note: I was typing up my reply as Steve posted his and therefore did not see it until I was done. So there's a little bit of redundancy. Great minds think alike and all that...}
That has nothing to do with using an escaping a character in the replacement string. That has to do with the regex itself. If you put a logging statement in, you will see that the find method is only finding the last line. You need to
indicate your pattern is multiline:
That will at least find each pattern that occurs on a separate line. But it will still identify this line:
all as a single match and not two matches.
As such
$1 = <c:param name="myName" value="<%= myValue %>" /> <c:param name="foo" value=" $2 = bar <-- with a trailing space $3 = " />
So the end of the first desired match and the start of the second desired match is matching the regex for the first capturing group, namely the .+
If we change that to a [^>]+ then we will only capture the desired start, but we'll also need to modify the third capturing group:
$1 = <c:param name="myName" value=" $2 = myValue <-- with a trailing space $3 = " /> <c:param name="foo" value="<%= bar %>" />
Now, the .* in the third group is capturing the second desired match. So we can change that to ("[^>]+> ;) and everything will work. Also, the [ \t]* should probably be changed to [\s]* since a carriage return in the middle of an XML element is legal as well as a space or tab.
Also, the regex does not take into account single quoted attribute values. (value = 'myValue'). So we want to change teh " to an option of ' or ".
So adding all our changes together we get:
That will do more along the lines of what you want. May not be perfect, I'd do some unit testing on it to check for other missed options.
There's a great tool available to help with writing regular expressions. It's called RegexBuddy. It does a lot. One of the nicest features is the ability to highlight not only the match, but what a capturing group is matching. It's a commercial product, but well worth the cost as it will save you hours of work.
[ August 25, 2008: Message edited by: Mark Vedder ]
Steve
Originally posted by Phil Powell:
Please break this down to a simpler level; I can't follow anything you posted whatsoever, sorry![]()
...
Originally posted by Piet Verdriet:
: p
Okay, you never gave an exact example in AND ouput, so I might be wrong, but if I understand you crrectly, you want to replace sub strings like this:
<c: param name="AAA" value="<%= aaa %>" />
into:
<c: param name="AAA" value="aaa" />
correct?
If so, you don't need to create any Matcher: replaceAll(...) on a String is sufficient.
Try this:
>> please explain ".*?" and ".+?", that sounds contradictory inasmuch as that sounds like "0 or more characters and 0 or 1 character" along with "1 or more characters and 0 or 1 character"; that does not make sense to me.
>> Thanks for the explanation on ?s and ?m
The (?s) and (?m) flags in front of the regex, tell replaceAll(...) to let the DOT match any character and matches can span over multiple lines.
[ August 27, 2008: Message edited by: Piet Verdriet ]
Originally posted by Phil Powell:
please explain ".*?" and ".+?", that sounds contradictory inasmuch as that sounds like "0 or more characters and 0 or 1 character" along with "1 or more characters and 0 or 1 character"; that does not make sense to me.
keep an eye out for scorpions and black widows. But the tiny ads are safe.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|