Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in regular expression 2

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I would like to test a String with regexp like this: The String can't contain numbers and the letters a-z or A-Z.
Which regexp String should I write?
 
I'll have a stab and let someone who knows what they're doing correct me [smile]
Code:
[^0-9a-zA-Z]

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Your question is a little ambiguous. Do you mean that the string can't contain both numbers and letters but can contain numbers without letters or letters without numbers, or do you mean that the string can't contain either numbers or letters at all?
 
The string can't contain numbers and ENGLISH letters. Letters in other languages it can contain.
 
The string can't contain numbers and ENGLISH letters

How are you going to differentiate between the letter 'A' in English and the letter 'A' in French, German, or most other latin-based alphabets ?

--------------------------------------------------
Free Database Connection Pooling Software
 
Well, the other letters are in Hebrew which means I don't need the latin-based alphabets... :)
 
So does my contribution work for you or not. Or am I invisible today?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
You haven't answered the question I posed, but I think this is what you mean: If the string can't contain numbers *or* English letters, you can do this:
Code:
if ( string.matches( "[^a-zA-Z0-9]+" ) ) {
   // the string is ok
}

 
Oh but now I've encountered a different problem:
I would like a match like this: [^a-zA-Z0-9]+ BUT if the number is not the first charcter of the String then keep it.
How do I do that?
 
What I mean is simply this:
I want the String to match this criterias:
It's first charcter can't contain 0-9 or a-zA-Z
What is the correct regexp for that?
 
Not sure if the \W expression is what you want.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hey timw,

What I mean is simply this:
I want the String to match this criterias:
It's first charcter can't contain 0-9 or a-zA-Z
What is the correct regexp for that?
 
It's getting beyond my knowledge a bit. How about
Code:
^[^0-9a-zA-Z]

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Maybe you should post a set of example strings which should fail the test and some which should pass.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
timw - that'd work in many languages. Java's String.matches() method puts an implied `^' and `$' around your regexp, so the regexp has to match the entire string, not a portion of it.

This is a slight alteration of my original regexp. This will match if the first character is not a-zA-Z0-9 and the rest of the characters are anything. (that's what the .+ is for).

Code:
if ( string.matches( "[^a-zA-Z0-9].+" ) ) {
   // the string is ok
}
 
Ahhhh, I see. Yes, that makes sense.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
By the way, if for example I want to check the second letter, I should do something like this ?
[^a-zA-Z0-9][^a-zA-Z0-9].+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top