• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble with Regex

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm using the eclipse scrap book (java) page and I'm getting the following
results:

String s = "2.3.42.33";

return s.replaceAll("(\\d)","/*\\1");

(java.lang.String) /*1./*1./*1/*1./*1/*1


What I want is "/*2./*3./*42./*33";

What am I doing wrong?

Thanks,
Siegfried
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your replacement String should be "/*$1" instead. You must have misread the documentation on replaceAll().

Also, now that I look at the output you're expecting you probably want (\\d+) not (\\d).
[ May 11, 2006: Message edited by: Ken Blair ]
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You must have misread the documentation on replaceAll()

Actually I'd have to give Siegfried a pass on that one. The documentation for the replaceAll() method in String class is severely lacking. (It doesn't even give a cursory description if the second parameter). It does say that it is implemented to yield the same results as the replaceAll() method in java.util.regex.Matcher class. If you look at that method, it says that the use of "$" in the replacement String refers to captured subsequences as in the appendReplacement() of Matcher class. Only in this method does it give a complete description of the use of the "$".
[ May 11, 2006: Message edited by: Garrett Rowe ]
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this one
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Garrett. The regex documentation requires work.

As for the regex, I would use...



Henry
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
I agree with Garrett. The regex documentation requires work.

As for the regex, I would use...



Henry



Out of curiousity, why is that notably better than "(\\d+)" "/*$1"?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a pretty minor difference, but Henry's version does avoid the (typically small) overhead of capturing an extra group we don't really need. Group 0 (the whole regex) is always captured. Additional capturing groups cost extra - if only slightly.

Regarding earlier posts: carefully following the documentation leads to this quote from Matcher.replaceAll():

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.


This is almost useful documentation, but for some reason the author decided to neuter it by repeated and totally unnecessary use of "may". Dollar signs (if followed by numbers) will be treated as references to captured subsequences. Too bad they didn't actually get around to saying that. :roll: Siegfried's confusion on this point is understandable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic