last day (14 days later) » 

11:09 AM
0
A: regex to check the space and underscore in the string

DharaniTo check string variable startswith space. ^\\s+ To check is variable has underscore between two words. [a-zA-Z0-9]+_[a-zA-Z0-9]+ Here str is String variable. Full code is: Pattern whitespace = Pattern.compile("^\\s+"); //whitespace match Pattern underscore = Pattern.compile("[a-zA-Z0-9]+_...

 
"...no underscore between words"
 
@Fildor yes, In full code I have mentioned !str.contains("_")
@baddger964 yes, sorry. I have updated. Thanks for that.
@sobrino yeah, sure. I will update with regex. Thanks for note.
 
@Dharani "In full code I have mentioned !str.contains("_")" which does not fulfill the requirement. It says: "no underscore between words" not: "no underscore".
 
@Fildor oopss, sorry. I will change. Thanks.
 
Now I am curious. IMHO this requirement will be nearly impossible to meet. Why? Because Java Regex considers "_" to be a legal "Word" character. So it could actually be part of a word. For example "nameWithNumbering_42". Now how should a regex validate this: "nameWithNumbering_42_Word"? Is it 3 words with underscores between? Is it one word?
 
11:09 AM
@Fildor please check now.
 
I'd say it is as close as you can get. You will still have false positives on underscores in words. But as I said. I cannot think of any decent way to make a distinction.
 
What the scenario it fails? Give me some exapmle. I will try to improve
@Fildor What the scenario it fails? Give me some exapmle. I will try to improve
 
Take the Example above. "name_42_word" - is it 3 words seperated by "_"? Is it "name_42" + "_" + "word" or "name" + "_" + "42_word" or is it "name_42_word" ?
If you exclude "_" from the word chars, "name_42" will always be considered 2 words with "_" between. Which differs from Java's definition of a "word".
For Java "name_42" is one single word.
 
11:46 AM
Imagine if the requirement said "there shall be no 'a's between words". How would you make regex decide that "name" is one word and not "n a me" and again "HouseaWork" are two words with an "a" between?
Of course you can say: "But 'a' is a letter, '_' is not". It is still considered a valid char in a word and thus equal to 'a' in that respect. Do I sound weird?
 

last day (14 days later) »