• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

regex question

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

It does not print anything. I was expecting to get these groups: \1 \2 \3
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you really mean "\1d\2df\3", or should that be "\\1d\\2df\\3"?
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Matthew, it does not print anything in both the cases. and I am confuse in understanding the difference in both.
What I trying to find is the patter with backslash and followed by single digit.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Needs more backslashes
The backslash is treated as an escape character by both the regular expression engine and Java.
Starting with you pattern definition, if your intent is to match a backslash followed by a single digit, you need \\\\ (four backslashes) to match the single backslash.
Of course that leaves the single digit, which can be represented by the escape character \d BUT seen as Java will also treat that \ as the start of an escape charcater, you need to escape it by adding another \, yeielding \\d.
Putting that toghether gives you the pattern \\\\\\d (six backslashes in there).
Now, that will fix the pattern, but it still won't yield a result, because there's something amiss with the literal String value you're trying to match against.
Why don't you try figuring out why that is from here

Edit: ugh, too slow.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If I do it with notepad++ and use the args[0] and args[1] at run time i have to pass these arguments args[0] = "\\\d" to find bachslsh and followed by a single digit. and If I use eclipse I use"\\\\d" so why this difference and also when use command line to pass pattern and matcher it works fine but not in eclipse.
Can you help in finding what am doing wrong? Thank you.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that \ is a special character in a Java string literal. Therefore, every time you include it in a string literal you need to escape it with another slash.

Since it's also a special character in regular expressions, when matching against a real slash you need to escape it. So if you are trying to put such a regular expression in a string literal you have to add both levels of escaping, hence the slash-happy example Jelle gave.

Input from the command line doesn't have the same issue.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jelle, Yes I found it and works fine Thank you.
Can you explain one more thing that why is it difference when I use notpad and then pass arguments at run time vs. eclipse?

Jelle Klap wrote:Needs more backslashes
The backslash is treated as an escape character by both the regular expression engine and Java.
Starting with you pattern definition, if your intent is to match a backslash followed by a single digit, you need \\\\ (four backslashes) to match the single backslash.
Of course that leaves the single digit, which can be represented by the escape character \d BUT seen as Java will also treat that \ as the start of an escape charcater, you need to escape it by adding another \, yeielding \\d.
Putting that toghether gives you the pattern \\\\\\d (six backslashes in there).
Now, that will fix the pattern, but it still won't yield a result, because there's something amiss with the literal String value you're trying to match against.
Why don't you try figuring out why that is from here

Edit: ugh, too slow.

 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew summed it up nicely in the reply above yours
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jelle and Matthew. one last question.

output

how the start index and end index work here?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, remember that because of the escaping, the actual string you're matching against is \\1\\2\\3 (you'd have got matches with pairs of slashes as well). So the following indicates the position of the matches
The start index is the start of the group (zero indexed), and the end index is the position immediately after the group.
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Matthew. Understood the point.
 
A feeble attempt to tell you about our stuff that makes us money
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic