File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Easy regular expression question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Easy regular expression question" Watch "Easy regular expression question" New topic
Author

Easy regular expression question

Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
I want to check if a certain string contains characters that I dont want to allow no matter where they are. Lets say I dont want to allow z or w in a String. Why does this not work ?

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You're trying to match the entire String to a pattern that expects only one character. Try adding a greedy quantifier.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Just tried for fun but don't know if this is correct way of doing it..
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
Stephan van Hulst wrote:You're trying to match the entire String to a pattern that expects only one character. Try adding a greedy quantifier.


Hmm I think I got a bit confused by the Javadocs I read
[^abc] Any character except a, b, or c (negation)
. I thought that ment if any of those characters are included the pattern will match.
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
John Jai wrote:Just tried for fun but don't know if this is correct way of doing it..


It seems to work.But my intuition says there is probably an easier way...:=)

Thanks anyway!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Yes.. having all those .*? is real fun
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
Actually...if the string value is empty we get the wrong result.

I think we need some help from a RegExp guru ;=) !
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Carlos Bonzilla wrote:I thought that ment if any of those characters are included the pattern will match.


No. When you match a String, it means that the entire String has to conform to the entire pattern, all at once. The [abc] part is a character class. Character classes only match one character in the input String, unless you quantify them. Essentially, what you need to say is: "I want my input String to match to [^zw]; zero or more times".

Now how could you do the zero or more times? Take a look at the greedy quantifiers part of the Javadoc.

Personally I would just do:
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
That is of course much easier. The problem though is that I am making an own JSF-component that should be able to take an arbitrary regular expression and match it to an arbitrary string.

The example I provided is just one example.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

But if your program accepts a regex and attempts to match a String to it, then whether or not it matches is out of your hands anyway, right? Or do you provide the regex yourself?

Anyway, for the problem you're currently facing, show me how you can solve it with a simple elegant regex. Hint: you only have to add one more character to the pattern you already had.
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
Yes the regex is provided as well.

My friend, if I knew how to do it I wouldn't have made a post ;=)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

It's all in the Javadoc. However, it may be easier to consult a tutorial. Take your time to read and fully understand all the lessons, it's worth it.

http://www.regular-expressions.info/tutorial.html
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
My friend, one of the main purposes with a forum is to get help quickly if needed.

If I would have needed a good tutorial then that would have been my question.

Besides that, I don't think there is a need for a tutorial unless you want to have a deeper understanding of the topic you question about :=) If I just want a qucik answer, reading a tutorial for 1 or 2 hours seems a bit overkill.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Carlos Bonzilla wrote:My friend, one of the main purposes with a forum is to get help quickly if needed.

If I would have needed a good tutorial then that would have been my question.

Besides that, I don't think there is a need for a tutorial unless you want to have a deeper understanding of the topic you question about :=) If I just want a qucik answer, reading a tutorial for 1 or 2 hours seems a bit overkill.


JavaRanch is a "learning" site. One of the objectives is to teach, or even better, get the candidate to learn for themselves (by pushing in the right direction). It is only "overkill" if you don't want to learn about the topic.


Regardless, please keep this in mind. JavaRanch is good to "get help quickly if needed" in many cases, but please don't be surprised if responders assumes that you actually want to learn the topic in question.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Carlos Bonzilla
Greenhorn

Joined: May 03, 2011
Posts: 17
Yes I agree. But I think it should be the candidates choice if he/she wants to learn something. Or just wants to be pushed in the right direction. I think the readers understood I wanted a quick answer (as Mr.Jai).

I thought it's a bit strange to have the answer as Mr.Van Hulst has and not writing it down and instead writing 3-4 replies that has nothing to do with my question.

Anyway, thank you all for your help.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

I already mentioned greedy quantifiers. I also already gave you a hint. Take ten minutes to browse over the Javadoc for the Pattern class and I'm quite sure you'll manage.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Carlos Bonzilla wrote:Yes I agree. But I think it should be the candidates choice if he/she wants to learn something. Or just wants to be pushed in the right direction. I think the readers understood I wanted a quick answer (as Mr.Jai).


First of all, I think it is a bad idea to assume what other people understood. Second, even if you are right, as mentioned, Java Ranch is a learning site. You need to expect some teaching. We simply don't just give out answers here. Sorry.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Easy regular expression question