Thursday, October 25, 2007

java: Short regular expression tutorial

^ = logical not

\w = shortcut for alphanumeric. i.e. a-z, A-Z, 0-9, _

- = dash

. = full stop

space bar pressed here = space bar

[\\w-. ] = regular expression for valid characters.

[^\\w-. ] = regular expression for not valid characters (i.e.invalid characters).


//find a character that is not valid (ie. invalid characters are a-z, A-Z, 0-9, _, dash, full stop, and space bar

String regex = "[^\\w-. ]";

---------------------------------------------------------

Below are the common symbols you will often find:

^ The beginning of the line.

$ The end of the line.

. Any single character.

* Zero or more repetitions of the previous expression.

+ One or more repetitions of the previous expression.

[] Any of the characters inside the brackets will match—e.g., [abc] matches any of a, b, c. Ranges are allowed too—e.g., [a-z].

1 comment:

Anonymous said...

Also

^ The beginning of the line.

$ The end of the line.

. Any single character.

* Zero or more repetitions of the previous expression.

+ One or more repetitions of the previous expression.

[] Any of the characters inside the brackets will match—e.g., [abc] matches any of a, b, c. Ranges are allowed too—e.g., [a-z].