• 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

Another Regex question with java

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I am rewriting a Perl script in Java.

I am using java.util.regex.Pattern and java.util.regex.Matcher for regex.

Now,




the value of bis false. I dont know why ? this regex works with perl and also with less.

But when I remove the comma (,) from the end of pattern, it returns true !

Can any body explain me why is this so and what should I do to overcome this ?

thanks,
Hardik
[ October 26, 2006: Message edited by: Hardik Mehta ]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm...

But when I remove the comma (,) from the end of pattern, it returns true !



My tests return false with or without the comma.

I think the problem has nothing to do with the comma. The issue is that your pattern does not account for the end of the string. Either:

"^S\\s+\\[(.*?)\\],.*" or "^S\\s+\\[(.*?)\\].*"

return m.matches = true and m.group(1) = "Oct 18 17:28:35"

I find this site very handy for testing java regex patterns.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hardik, the results you describe don't seem to match up with the code you show. Like Bridget, I get false either way. I recommend you compare the API documentation for matches() adn find() - I think find() is probably what you really want here.
 
Hardik Mehta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

thanks for the reply.

And you are right, when I used it without comma, I used ".*" at the end. And I found out that in Java you always need to specify it and in Perl not.

anyway thanks.

regards,
Hardik Mehta
 
Hardik Mehta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Another question :

now I have



and the output is



can anybody tell me what is wrong with the regex and what should I change to get 1100 in the 5th group ?

regards,
Hardik
[ October 26, 2006: Message edited by: Hardik Mehta ]
 
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
You kind of messed up your regex around here...



This is different from all the other regex sections that you used. It also looks like you did some weird some with the "?", and with close parens later, as little makes sense after this point.

I would also suggest that you qualify the regex a bit, when you are using ".+" for all groups, it is hard to control what goes in what groups -- as the regex will just greedily grab as much as possible.

Henry
 
Hardik Mehta
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

the expression ,



means that here I am expecting a group (A 1234) or (A ) or the whole group may be absent. But in case, the group is present, I want to extract it as group no. 6.

I think now this makes sense.

Now you are right about qualifying and this solved my problem. Following pattern,



works well and I have correct value for V or group 5.

But still, I wonder how the same pattern works for R or group 4 but not for V or group 5. I think this is because of the weird group A.

But anyway, this has solved my problem and I thank you all who took pain to answer me.

regards,
Hardik
 
Does this tiny ad smell okay to you?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic