Java validation with regular expression

Hi

I need to validate a string such that only numbers, alphabets, -, _, . is allowed and the below mentioned code works fine.

if( ! l_name.matches("[A-Za-z0-9._-]+" ) ) {

// do stuff when a valid name is found

}

But i need to modify the above regex such that it should also allow space in the name. Any idea how to do that?

Thanks in advance.

[386 byte] By [Sujiea] at [2007-11-27 10:55:37]
# 1

Ummm... stick a space in the regex... jee that was hard... it must be beer o'clock.

corlettka at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...
# 2

I tried a space it does not work!!!

Sujiea at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...
# 3

Ummmm... yes it does... where's that second beer.

corlettka at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...
# 4

> I tried a space it does not work!!!

Then you added the space between a character range or something.

Here, this is a bit more compact:name.matches("[-.\\w\\s]+")

prometheuzza at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi,

try the following regex pattern. it will solve it.

"[\\w-.\\s]+"

Regards,

Loga

Loga_07a at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...
# 6

LOL! It's the absolute same regex from the previous reply :P

jadespirita at 2007-7-29 11:57:39 > top of Java-index,Java Essentials,Java Programming...