aspose file tools*
The moose likes Beginning Java and the fly likes regular expression doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "regular expression doubt" Watch "regular expression doubt" New topic
Author

regular expression doubt

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I am trying to find whether letters A,b or * exists in a string.


But it doesnot seem to work. Please let me know.
Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

You can Try This



Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38849
    
  23
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 *.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
What is the result of

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

?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Kuldeep Yadav:
You can Try This



Thanks


Pattern.matches would work for a regular expression.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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

Joined: Feb 25, 2006
Posts: 266
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
Sheriff

Joined: Oct 13, 2005
Posts: 38849
    
  23
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jan 13, 2008
Posts: 242
Thanks for the replies.

After I trim the testString, it works

String testString = enteredString.trim();

Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Jan 13, 2008
Posts: 242
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

Joined: Feb 25, 2006
Posts: 266
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Feb 25, 2006
Posts: 266
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.
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
I won't say much.. but check this program..

Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Feb 25, 2006
Posts: 266
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
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

Joined: Feb 25, 2006
Posts: 266
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
Ranch Hand

Joined: Mar 05, 2008
Posts: 3014
    
  10
[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

Joined: Feb 25, 2006
Posts: 266
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regular expression doubt