• 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

regular expression doubt

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to find whether letters A,b or * exists in a string.


But it doesnot seem to work. Please let me know.
 
Ranch Hand
Posts: 162
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can Try This



Thanks
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to go through the regular expressions part of the Java Tutorials. The * is a metacharacter, so you might need to escape it.

The replaceAll method won't tell you whether your String contained those letters; contains will, but you would have to use it thrice to pick up A b or *.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the result of

"C".replaceAll("[Ab*]","").length()

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

Originally posted by Campbell Ritchie:
You need to go through the regular expressions part of the Java Tutorials. The * is a metacharacter, so you might need to escape it.

The replaceAll method won't tell you whether your String contained those letters; contains will, but you would have to use it thrice to pick up A b or *.



It appears you should also go through the regex tutorial since '*' has no special meaning inside a character class.


@OP: try this:


[ August 14, 2008: Message edited by: Piet Verdriet ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piet Verdriet:


@OP: try this:



[ August 14, 2008: Message edited by: Piet Verdriet ]



How is that going to help?
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kuldeep Yadav:
You can Try This



Thanks



Pattern.matches would work for a regular expression.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:


How is that going to help?



It's answers the OP's question
Perhaps you need to reread it.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
I am trying to find whether letters A,b or * exists in a string.


But it doesnot seem to work. Please let me know.



Gopu, as I suggested, you need to use the negation character, [^...], in the character class*:



Of course, there are other (or better) ways to see if some character is present inside a String, but the code above answers your question.

* Note that outside a character class, the '^' character normally means the beginning of the String, and sometimes the beginning of a line.
[ August 14, 2008: Message edited by: Piet Verdriet ]
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Piet Verdriet:
It appears you should also go through the regex tutorial since '*' has no special meaning inside a character class.
[ August 14, 2008: Message edited by: Piet Verdriet ]

Will do next time I need a regex.
 
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet's suggestion should work. However I would note that there's another solution using the original regex exactly as given - you just need to modify another part of the test. I think that's what Ilja was hinting at when he asked

What is the result of

"C".replaceAll("[Ab*]","").length()

?



Comparing the length to 0 won't give the desired result, but there's something else you can compare to that will work.

Or, of course, you can also use Pattern.matches(), as Ilja also said.
[ August 14, 2008: Message edited by: Mike Simmons ]
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the replies.

After I trim the testString, it works

String testString = enteredString.trim();

 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the code you just showed (the last line) is still operating on enteredString, not on your new trimmed testString. And that code certainly won't work for most inputs. I hope this means you simply copied the code incorrectly. Otherwise it may mean you need better tests.
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Mike, something went wrong in copying the codes as my real variable's name is different.

works. Thanks for the clarification.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Gopu Akraju:
Sorry Mike, something went wrong in copying the codes as my real variable's name is different.

works. Thanks for the clarification.



No, that will also return true for a String like "zzz", for example.
Again, to see if a String contains 'A', 'b' or a '*', using replaceAll(...), you need to do it like this:
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Piet, there exists at least one String for which your code is giving the wrong result. (Hint: think of a very minimal border case.)
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Piet, there exists at least one String for which your code is giving the wrong result. (Hint: think of a very minimal border case.)



Well, I'll be d.....!


It's probably a "but of course!" thing, but I can't figure out which case. Could you spill the beans?

Thanks.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I won't say much.. but check this program..

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

Originally posted by Ilja Preuss:
Piet, there exists at least one String for which your code is giving the wrong result. (Hint: think of a very minimal border case.)



Ilja, just so that we are talking about the same here. You say there exists (at least one) String containing at least one of the three characters 'A', 'b' or '*' and that String returns false for:



Or, of course, the other way around: there exists (at least one) String containing none of the characters 'A', 'b' or '*' and that String returns true for:



Good sir, don't torment me any longer! I can't figure it out!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that I get for not writing tests for my assumptions. I was wrong. Sorry. My excuse is that I didn't have enough sleep... (I was thinking of the empty String, of course.)

You know what, I'll take that as an argument that the code isn't expressive enough, and that my Matcher idea is better

No, seriously, sorry again for tormenting you unnecessarily.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
OK, that I get for not writing tests for my assumptions. I was wrong. Sorry. My excuse is that I didn't have enough sleep... (I was thinking of the empty String, of course.)



I was thinking of a backspace character before one of the characters 'A', 'b' or '*', but that didn't work either.

You know what, I'll take that as an argument that the code isn't expressive enough, and that my Matcher idea is better



True: using replaceAll(...) to mimic contains(...) or matches(...) functionality, is a rather strange way to do this.

No, seriously, sorry again for tormenting you unnecessarily.



No matter!

[ August 16, 2008: Message edited by: Piet Verdriet ]
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for completeness, the other solution I mentioned is to modify the original code thus:

Comparing the length to zero made no sense. But comparing to the original length, to see if it's changed - that works fine.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
Just for completeness, the other solution I mentioned is to modify the original code thus:

Comparing the length to zero made no sense. ...



It does make sense when negating the three characters inside the character class as I posted more than once.
But as noted before (by many posters) replacing characters, with or without using negation, is rather unintuitive: better use a contains(...) or Matcher.find(...) approach.
 
Mike Simmons
Master Rancher
Posts: 4796
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Piet]: It does make sense when negating the three characters inside the character class as I posted more than once.

Yes, obviously. I was referring to the original code.

[Piet]: But as noted before (by many posters)

Including me.

replacing characters, with or without using negation, is rather unintuitive: better use a contains(...) or Matcher.find(...) approach.

Yes, again, obviously.

I was tying up a loose end: I had previously stated that there was another solution. I think Ilja may have been hinting at it as well, since his first question was getting the poster to think about what the length really was (i.e., not zero). And Gamini Sirisena later posted the same solution, buried in the code. While this might not seem the most straightforward solution to you, or to me, or to Ilja or Gamini (probably), it does seem pretty close to what the original poster was thinking in the first place. Ilja and I were trying to get the poster to think more carefully about what was happening there, rather than simply giving them the answer.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Simmons:
...
I was tying up a loose end:
...



Ah, I see. Thanks for your explanation.
[ August 17, 2008: Message edited by: Piet Verdriet ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic