I need a regular expression pattern to check a string for alphanumeric(a-zA-z0-9) and also can contain underscore, hypen and dot this will be a file name so i dont want other character than this.
A:
Re:
I need a regular expression pattern to check a string for alphanumeric(a-zA-z0-9) and also can contain underscore, hypen and dot
The pattern will be "[_a-zA-Z0-9\\-\\.]+"
Note the double-backslashes, since this is first interpreted as a Java string and the backslashes need to stay intact for the regex.
danben
2010-01-13 19:33:30
A:
Check out "Character Classes" in the docs. Basically, [a-zA-Z0-9_\-\.]+
should do it. If you put that in a string, be sure to escape the backslashes.
T.J. Crowder
2010-01-13 19:33:49
the string can also have whitespace, example "program files"
2010-01-13 19:46:03
@unknown: Then add it to the range. See the link for details.
T.J. Crowder
2010-01-13 19:47:06
on *nix filenames can contain just about everything except "/", including control characters and the like.
Bryan Oakley
2010-01-13 23:03:14
@Bryan: I know, and there are lots of other valid characters even on Windows. I was just answering the question asked. :-)
T.J. Crowder
2010-01-13 23:14:22