f



Regular expression fails to replace but matches

<pre>
<code>
// PATTERN: (<c:param.+value=")<%=[ \t]*([^%]+)[ \t]*%>(".*$)
  Pattern p = Pattern.compile("(<c:param.+value=\")<%=[ \\t]*([^ \\t%]
+)[ \\t]*%>(\")");
  Matcher matcher = p.matcher(stuff);
  Pattern p2 = matcher.pattern();
  System.out.println(p2.pattern());
  if (matcher.find()) {
   System.out.println("Found erroneous pattern \"<%= %>\" within \"" +
file.getName() + "\", converting now");
   stuff = matcher.replaceAll("$1${$2}$3");
  }
</code>
</pre>

This code works to find the JSTL tag pattern:

<pre>
 <c:param name="[whatever]" value="<%= [whatever else] %>" />
</pre>

But no pattern substitution takes place.  I do not understand why this
fails to replace the pattern but successfully finds the pattern.  Any
ideas why?  Is this more proof that Java can't handle regular
expressions?
0
8/25/2008 2:56:45 PM
comp.lang.java.help 13339 articles. 0 followers. Post Follow

5 Replies
354 Views

Similar Articles

[PageSpeed] 6

On 25/08/2008 16:56, phillip.s.powell@gmail.com allegedly wrote:
> <pre>
> <code>
> // PATTERN: (<c:param.+value=")<%=[ \t]*([^%]+)[ \t]*%>(".*$)
>   Pattern p = Pattern.compile("(<c:param.+value=\")<%=[ \\t]*([^ \\t%]
> +)[ \\t]*%>(\")");
>   Matcher matcher = p.matcher(stuff);
>   Pattern p2 = matcher.pattern();
>   System.out.println(p2.pattern());
>   if (matcher.find()) {
>    System.out.println("Found erroneous pattern \"<%= %>\" within \"" +
> file.getName() + "\", converting now");
>    stuff = matcher.replaceAll("$1${$2}$3");
>   }
> </code>
> </pre>
> 
> This code works to find the JSTL tag pattern:
> 
> <pre>
>  <c:param name="[whatever]" value="<%= [whatever else] %>" />
> </pre>

No it doesn't. Have you even bothered to test that code?

1. In your Pattern, second capturing group:
   "([^ \\t%]+)"

If there's a whitespace in that character class, it won't match the
input above.

2. I'd suggest you be explicit about greediness. I'd suggest the
following Pattern instead of the one you used:
(<c:param.+?value=\")<%=[ \\t]*([^\\t%]+?)[ \\t]*%>(\")
-----------^----------------------------^ explicit reluctance

3. Replacement String: "$1${$2}$3". What group reference is "${$2}
supposed to be? That's not legit.

-.-

Bottom-line: before you happy-go-lucky around claiming that "Java can't
handle regular expressions", you should rather concern yourself with
whether your code actually does what you claim it does, and whether it
is even valid to begin with.

-- 
DF.
0
8/25/2008 11:17:23 PM
On Aug 25, 7:17=A0pm, Daniele Futtorovic <da.futt.n...@laposte.invalid>
wrote:
> On 25/08/2008 16:56, phillip.s.pow...@gmail.com allegedly wrote:
>
>
>
>
>
> > <pre>
> > <code>
> > // PATTERN: (<c:param.+value=3D")<%=3D[ \t]*([^%]+)[ \t]*%>(".*$)
> > =A0 Pattern p =3D Pattern.compile("(<c:param.+value=3D\")<%=3D[ \\t]*([=
^ \\t%]
> > +)[ \\t]*%>(\")");
> > =A0 Matcher matcher =3D p.matcher(stuff);
> > =A0 Pattern p2 =3D matcher.pattern();
> > =A0 System.out.println(p2.pattern());
> > =A0 if (matcher.find()) {
> > =A0 =A0System.out.println("Found erroneous pattern \"<%=3D %>\" within =
\"" +
> > file.getName() + "\", converting now");
> > =A0 =A0stuff =3D matcher.replaceAll("$1${$2}$3");
> > =A0 }
> > </code>
> > </pre>
>
> > This code works to find the JSTL tag pattern:
>
> > <pre>
> > =A0<c:param name=3D"[whatever]" value=3D"<%=3D [whatever else] %>" />
> > </pre>
>
> No it doesn't. Have you even bothered to test that code?
>
> 1. In your Pattern, second capturing group:
> =A0 =A0"([^ \\t%]+)"
>
> If there's a whitespace in that character class, it won't match the
> input above.
>
> 2. I'd suggest you be explicit about greediness. I'd suggest the
> following Pattern instead of the one you used:
> (<c:param.+?value=3D\")<%=3D[ \\t]*([^\\t%]+?)[ \\t]*%>(\")
> -----------^----------------------------^ explicit reluctance
>
> 3. Replacement String: "$1${$2}$3". What group reference is "${$2}
> supposed to be? That's not legit.
>
> -.-
>
> Bottom-line: before you happy-go-lucky around claiming that "Java can't
> handle regular expressions", you should rather concern yourself with
> whether your code actually does what you claim it does, and whether it
> is even valid to begin with.

It was very valid, well, until I decided to use an incorrect pattern
syntax, loop via matcher.find() and use matcher.replaceFirst("$1\\
{$2}$3") - and BTW it IS legit, the braces are intentional

And now it works, though it shouldn't

>
> --
> DF.- Hide quoted text -
>
> - Show quoted text -

0
8/26/2008 9:38:44 PM
On 26/08/2008 23:38, phillip.s.powell@gmail.com allegedly wrote:
> It was very valid, well, until I decided to use an incorrect pattern
> syntax, loop via matcher.find() and use matcher.replaceFirst("$1\\
> {$2}$3") - and BTW it IS legit, the braces are intentional

Yes, but the dollar sign before them isn't (which doesn't appear here,
but it did in the code you posted originally).

-- 
DF.
0
8/26/2008 9:44:52 PM
On Aug 26, 5:44=A0pm, Daniele Futtorovic <da.futt.n...@laposte.invalid>
wrote:
> On 26/08/2008 23:38, phillip.s.pow...@gmail.com allegedly wrote:
>
> > It was very valid, well, until I decided to use an incorrect pattern
> > syntax, loop via matcher.find() and use matcher.replaceFirst("$1\\
> > {$2}$3") - and BTW it IS legit, the braces are intentional
>
> Yes, but the dollar sign before them isn't (which doesn't appear here,
> but it did in the code you posted originally).

Right, because I want to change

<c:param name=3D"key" value=3D"<%=3D value %>" />

Into

<c:param name=3D"key" value=3D"${value}" />

>
> --
> DF.

0
8/27/2008 1:18:10 PM
On 27/08/2008 15:18, phillip.s.powell@gmail.com allegedly wrote:
> On Aug 26, 5:44 pm, Daniele Futtorovic <da.futt.n...@laposte.invalid>
> wrote:
>> On 26/08/2008 23:38, phillip.s.pow...@gmail.com allegedly wrote:
>>
>>> It was very valid, well, until I decided to use an incorrect pattern
>>> syntax, loop via matcher.find() and use matcher.replaceFirst("$1\\
>>> {$2}$3") - and BTW it IS legit, the braces are intentional
>> Yes, but the dollar sign before them isn't (which doesn't appear here,
>> but it did in the code you posted originally).
> 
> Right, because I want to change
> 
> <c:param name="key" value="<%= value %>" />
> 
> Into
> 
> <c:param name="key" value="${value}" />

Then you must escape the dollar sign: "$1\\${$2}$3". It's a
metacharacter in the replacement sequence.

-- 
DF.
0
8/27/2008 1:58:02 PM
Reply:

Similar Artilces:

Regular expression to match regular expressions?
An expression parser (in TAWK) accepts textual regular expressions, which are then converted to internal form by TAWK's regex() function. If the regular expression is ill-formed, then TAWK's run-time package issues an error message on the console (rather than, say, returning a null value from regex()). Is there a cheap way to verify that a regular expression is properly formed *before* feeding it to regex(), so as to avoid that error message? A first thought: regex = /^\/.+\/i?$/ - starts and ends with '/' or '/i' (i-gnore case), must be non-null A second thought:...

Help on regular expression match
Hi, I've met a problem in match a regular expression in python. Hope any of you could help me. Here are the details: I have many tags like this: xxx<a href="http://xxx.xxx.xxx" xxx>xxx xxx<a href="wap://xxx.xxx.xxx" xxx>xxx xxx<a href="http://xxx.xxx.xxx" xxx>xxx ..... And I want to find all the "http://xxx.xxx.xxx" out, so I do it like this: httpPat = re.compile("(<a )(href=\")(http://.*)(\")") result = httpPat.findall(data) I use this to observe my output: ...

Java Regular Expressions Help
Hi there, I wonder if anyone can tell me how to set a boolean if I encounter a word in a string (in upper, lower, or a combination of cases) e.g. I want to set a boolean to true if either "hello", "HELLO", or "hElLo" is found in "Hello there!". So far, I have Pattern p = Pattern.compile("HELLO"); Matcher m = p.matcher("HELLO there!"); myBoolean = m.lookingAt(); Can anyone tell me how to modify the above so that is will match true for upper/lower case, and will return true even in the case: " 4HELLO123" Stumped!...

help needed with regular expression in java
Hi, I need to match Strings like: cleartool: Error: Element "M:\myview\acme\src\Test.java" has checkouts." or cleartool: Error: Element "/vobs/acme/src/Test.java" has checkouts." How can I write a regular expression that matches this when a variable file name. I have other Strings that start with 'cleartool: Error: Element ....' I only need help with the actual regexp in java. cheers, //mike mike wrote: > Hi, > > I need to match Strings like: > > cleartool: Error: Element "M:\myview\acme\src\Test.java" has > checkouts." > > or > > cleartool: Error: Element "/vobs/acme/src/Test.java" has checkouts." > > How can I write a regular expression that matches this when a variable > file name. > > I have other Strings that start with 'cleartool: Error: Element ....' > > I only need help with the actual regexp in java. You haven't really described what you want to do -- you may think you have, but you haven't. Just saying that you want to match Strings that are "like" a couple examples doesn't tell us enough; you've got to describe how the Strings you want to match differ from those you don't want to match. In fact, that's exactly what the regular expression does: It's a precise description of the difference between matching and non-matching Strings. If you can't describe that difference yourse...

Regular Expression and string Matching/Replace
I have a list of key words. It has around 1000 key word now but can grow to 5000 keywords. My web application displays lot of texts which are stored in the database. My requirement is to scan each text for the occurance of any of the above keywords. If any keyword is present I have to replace that with some custom values, before showing it to the user. I was thinking of using using regular expression for replacing the keyword in the text using matcher.replaceAll method as follows: Pattern pattern = Pattern.compile(patternStr); Matcher matcher = pattern.matcher(inputStr)...

Regular Expression help
Hi, I have a text string looking something like this: my $test = "<a bookmark></a> en href=\"http://www.google.com\" dette er en test for at checke <a target=\"_new\" href=\"http://www.google.dk\" prop=\"saya\"> NNIT<\\a> link test og lidt mere tekst til :-) <a href=\"www.saya.dk\" target=\"new\">saya<\\a> den replacer garanteret ogs� <a href=\"/saya.asp\">ASP<\\a>"; In the above their are 4 links: 1. "<a bookmark></a> en href=\"http://www.g...

Help needed with tough regular expression matching
Hello all, My initial task was to match the well-known notation used in math and other fields to denote integer sequences. Such notation is nice, unambiguous and -some times- succinct: 1,2,3,4,8-10,11-14 which is equivalent to: 1-4,8-14 (and to may other possibilities, the one immediately above being the most succinct possible) I have been able to come up with a regular expression which unequivocally matches any valid "statement" of such "language". See below. My problem is that now I would like to write a more general form of such sequence matching. The "base...

Replacing part of a matched regular expression using gsub
Hi, I'm trying to run through a piece of text replacing every character with a context sensitive number. Rather than go into the gory details, hopefully the following will enough to make clear what I'm trying to do. Today is the first day I've written any ruby code, so please forgive me if this is an incredibly dumb post! Anyway, my aim is to find a character, let's say 'x' in the context of two other characters, let's say 'a' and 'b'. When I find 'axb' I want to replace x with a number and a comma, so I should end up with 'a102,b'...

sed regular expression search & replace help.
Hi, I am trying to setup some bash scripts do a search & replace in some files using sed. I am running into difficulties because the files contain text which has speech marks ("), which need to be included in the regular expression. This forced me to use single quotes (') for the main bit of the sed script. However as you can see from the example below I need to insert an argument ($1) from the bash script into the replacement part of the regular expression, and this does not seem to work when using single quotes (') to contain the regular expression in the command line. se...

RE: Help With EOF character and regular expression matching: URGENT
> I want to strip off the headers, like: To From > Returned Path etc... Have a look at the 'email' module in the Library. > and also the characters that are not ASCII and also > the characters that are between <> so as to avoid HTML > Tags. > I have zero experience with regular expressions > but if you or some one can give me an idea/snippet I > think I can make it work. import re text =3D "look ma, <b>no</b> html!" cleaned =3D re.sub(r'<[^>]*>', '', text) print cleaned > Also while I can write the word...

Matching abitrary expression in a regular expression
Hi, How to match a mathematical expression in a single regular expression? moopT wrote: > How to match a mathematical expression in a single regular expression? You can't. Mathematical expressions make use of nested brackets. Regexps don't have the power to handle arbitrarily nested constructs. -- chris moop� wrote: > Hi, > How to match a mathematical expression in a single regular expression? ..* Chris Uppal <chris.uppal@metagnostic.REMOVE-THIS.org> wrote: > You can't. Mathematical expressions make use of nested brackets. Regexps > don't ...

How to print all expressions that match a regular expression
Hi, I am a fresh man with python. I know there is regular expressions in Python. What I need is that given a particular regular expression, output all the matches. For example, given =93[1|2|3]{2}=94 as the regular expression, the program should output all 9 matches, i.e., "11 12 13 21 22 23 31 32 33". Is there any well-written routine in Python or third-party program to do this? If there isn't, could somebody make some suggestions on how to write it myself? Thanks. Zhuo In article <ee2cfd35-3171-4ee7-ad3a-cf117e5527c5@r24g2000yqd.googlegroups.com>, "hzhuo1@gmai...

Re: Help With EOF character and regular expression matching: URGENT #2
> Currently I have one problem and I dont know if there > are any good ways to solve it in python: > = > I want to create a dictionary of words out of the spam > datasets and legitimate email datasets. While I can > extract each and every word from the spam and > legitimate emails it is not advisable to do so. I want > to strip off the headers, > like: > To > From > Returned Path > etc... > and also the characters that are not ASCII and also > the characters that are between <> so as to avoid HTML > Tags. > I have zero experience with r...

Regular Expression Help
Hi, I have some code to perform matches for a UK postcode. UK postcodes have two sections the first section representing a particular geographic area. e.g. B8 2ER Where the B8 - represents an area in Birmingham and the second section 2ER would represent an area within B8. In my application the two sections are stored in different fields in the MySQL DB (postcode_1 and postcode_2 in my case). I wrote some code to show all customers that fall within certain postcodes and I done this by having script which says WHERE postcode_1 LIKE "B%" to find all the customers within the Bi...

Regular Expression help
Hi On our way to shifting to Ruby 1.8.7 - away from the beloved Oniguruma-Patch - I am tripping over our usefull regular expression used in Ruby 1.8.6 - so if anybody is strong in refactoring regular expressions, please let me know. sample one: http://choddb.rubyforge.org/ODDB/SwissmedicPlugin.html %r{excipiens\s+(ad|pro)\s+(?<galform>((?!\bpro\b)[^.])+)}u and %r{pro\s+(?<scale>(?<qty>[\d.,]+)\s*(?<unit>[kcmu=C2=B5n]?[glh]))}u And this one on line 148 http://url.ba/kxez All three samples need to work in Ruby 1.8.7 and in 1.9.2. They curr...

Need help with a regular expression to replace all commas with spaces (except those enclosed within braces)
Hi I need help with a regular expression that can replaces all commas (,) in a string with spaces except for those enclosed within braces: I/P: {a,b,{c,d},e,f} O/P: a b {c,d} e f Also, if there are nested braces, can we handle that as well: I/P: {a,b,{x,{c,d},y},e,f} O/P: a b {x,{c,d},y} e f Any help in this regard would be much appreciated. Thanks Sharad On 18/03/2013 07:58, sharad1087@gmail.com wrote: > I need help with a regular expression that can replaces all commas (,) > in a string with spaces except for those enclosed within braces: That's quite ...

s/$match/$replace/ fails when $replace has backreferences
I need to store my match and replace strings in variables. This fails when my match string uses back references to groupings. Here for example I try to replace "foo" with "foobar" using a back reference: #!/usr/bin/perl $content = "this is my foo"; $match = "(foo)"; $replace = "\1bar"; #$replace = "\\1bar"; # this doesn't work either #$replace = "$1bar"; # neither does this $content =~ s/$match/$replace/; print "$content\n" Any ideas how to get this to work? I need the match and replace strings in variab...

regular expression no match on set if subset matches
This may be simple but I can't seem to get a handle on how to do the following: I am trying to define a regular expression to validate a password format so that the password is valid if it contains from 4 to 10 characters except in the one special case where it has the format of three letters followed by two numbers. That is, "abc13edl" is fine but "abc13" is not. Is there a solution to this (using the gnu Java regular expression package)? Any help greatly appreciated! "Dave" <daverawding@attbi.com> wrote in message news:6f89a035.0401231902.30c45a3...

Help with Regular Expressions
I have been looking around the web for a good list of the basic regex key characters / commands, not just examples. ie. \w = word .. = any character + = one or more etc. etc.etc If anyone can point me in the right direction I would be very grateful Thanks ian "Bimmian" <bimmian@gmail.com> wrote in message news:453ee400.0409251014.4ed8b0d8@posting.google.com... > I have been looking around the web for a good list of the basic regex > key characters / commands, not just examples. > > ie. \w = word > . = any character > + = one or more > etc. etc.etc >...

Java Regular Expressions
http://www.javaregex.com : Enables a java application or applet to compile and use perl5 regular expressions. It also provides tools to use the object-oriented capabilities of java to extend these expressions simple ways. In addition, it provides a special interface for globbing or matching file names. On 19 okt, 13:30, kkk <kuddin...@gmail.com> wrote: > http://www.javaregex.com: Enables a java application or applet to > compile and use perl5 regular expressions. It also > provides tools to use the object-oriented capabilities of java to > extend these expressions simple way...

Help Regular Expression
Hello Guys, Trying to analyse my web site stats, I tried to exclude from the analysis all the URL that contain fr/ or de/ (Exemple: www.mysite.com/fr/home.php or www.mysite.com/de/home.php). RegExp are not my favorite topic and I couldn't manage to create a correct expression to exclude such directories. Any help would be appreciated! Thanks folks. Thiegau thiegau <thiegau@gmail.com> wrote: > Hello Guys, > Trying to analyse my web site stats, I tried to exclude from the > analysis all the URL that contain fr/ or de/ > (Exemple: www.mysite.com/fr/home.php or www.m...

Regular expressions help
Given the following three lines: name fredgeorge name georgeharry name george I'm trying to find a regular expression that matches only the third one. My book resources are pretty skimpy when it comes to regular expressions, and the stuff on the web I've found doesn't cover this circumstance, and I can't figure out how to extrapolate from what is described to my problem. Can anyone help? Thanks! Look in the module reference for REs, particularly the expression syntax section: http://www.python.org/doc/2.3.3/lib/re-syntax.html You probably want either the $ or \b con...

Regular Expression help
Hi All, I am trying to filter users that are invalid from this list (see example below). What is the correct regular expression that I can apply using 'grep' or 'sed'? 23604 c_weberd3 23607 ro'donnell_1 43692 clskipper,jr. List of valid characters are: alphanumeric, hyphen, underscore. So, in the sample list above, entry 1 is valid, while entries 2 (it has a single-quote) and 3 (it has a comma) are invalid. thanks! --kishore kishore puvvada wrote: > > Hi All, I am trying to filter users that are invalid from this list > (see e...

The power of regular expressions without regular expressions.
I'm not talking here about the relatively simple, quick and dirty, one-time regular expressions that you might interactively feed to grep. Rather, I'm talking about the more complex regular expressions that might appear in your Java programs. For many of us, regular expressions that are not fairly simple, are an unreasonable challenge, are effectively "write only" and so are virtually unmodifiable. Are you tired of escaping escapes of escapes and of things like " (.*) (\\d+|@|_)\\[(\\S+)( +)((-?)(\\d+\\.\\d\\d))" and "((?:(?:[012]?[0-9]):)?)((?:(?:[0-5]?[0-9]):)?)((?:[0-5]?[0-9]))" Then try Naomi, a system that gives you the power of regular expressions with nary a single regular expression. Here is a fragment of Naomi code: // Spec is an abstract class. // Every instance of Spec matches certain strings. //Matches the string,".*" No need to escape the '.' or the "*" Spec star=new CharSequenceSpec(".*"); // Matches "p","q","r","s" or "t" Spec letter=new CharClassSpec(new IntervalCharClass(false,'p','t')); // Force to match 0 or more instances of "p","q","r","s" or "t" letter.setMinCount(0); letter.setMaxCount(null); //null means infinity Spec sharps=n...

Web resources about - Regular expression fails to replace but matches - comp.lang.java.help

Expression (sign language) - Wikipedia, the free encyclopedia
Signs with two different expressions. The pursed lips and partly closed eyes on the left, and raised lip on the right, are necessary for proper ...

Turkey setting ‘poor example’ for freedom of expression: Biden
Turkey setting ‘poor example’ for freedom of expression: Biden

Christie’s Expressions At Trump Event Under Scrutiny
Donald Trump Holds Super Tuesday Election Night Press Conf. In Palm Beach PALM BEACH, FL - MARCH 01: Republican Presidential frontrunner Donald ...

Facial Expressions and Tail Expressions
Submitted by: (via Daniel J Farrell ) Tagged: dogs , face , expression , tail , wag Share on Facebook

Duke Student Columnist: America’s First Amendment ‘Obsession’ Is ‘Expression Of White Supremacy’
Duke Student Columnist: America’s First Amendment ‘Obsession’ Is ‘Expression Of White Supremacy’

US: Joe Biden slams Turkey for ‘poor example’ of freedom of expression
US Vice President Joe Biden has criticised Turkey for “setting a poor example” in the region concerning freedom of expression. The damning comments ...

Decoding The Facial Expressions Of The GOP Candidates
Trump's "crooked contemptuous" smile, Rubio's sheepish grin, and Cruz's upper lip of disgust dominated the debate. For tonight's Republican ...

You enjoy freedom of expression only because army guards borders: Delhi HC to Kanhaiya Kumar
The bail granted to Kanhaiya Kumar comes with several conditions, directives to JNU faculty and some serious observations

Bernie Sanders Brought His Finest Facial Expressions to the Democratic Debate
You may not like Bernie Sanders as a presidential candidate, but you’ve gotta admit, this man’s got some pretty expressive peepers. Read more... ...

Crosswords Help You Learn Regular Expressions
Regular expressions might seem arcane, but if you do any kind of software, they are a powerful hacker tool. Obviously, if you are writing software ...

Resources last updated: 3/9/2016 10:20:32 AM