Java - general

regular expressions

Here a little extract out of the possibilities of regular expressions (regular expressions):



  • ^ standing lonely it represents the beginning of a character string
  • . it represents any sign
  • + the previous sign must appear at least once (therefore, .+ represents any character string with at least one sign)
  • * the previous sign can appear not once to any often (therefore, .* represents any character strings)
  • [abc] one of the signs in the brackets must appear instead of the bracket-expression („a[12]b“ can match „a1b“ and „a2b“)
  • [^abc] none of the signs in the brackets is allowed to appear instead of the bracket-expressionkeines („a[^12]b“ cannot match „a1b“ and „a2b“, but „a3b“)
  • [a-c] one of the signs in the stated area must appear instead of the bracket-expression („a[1-3]b“ matches „a1b“, „a2b“ and „a3b“, but not „a4b“)
  • [^a-c] none of the signs in the stated area is allowed to appear instead of the bracket-expression
  • ? the precedent sign is allowed to appear once at the most („a?b“ matches „ab“ and „b“, but not „aab“).
  • \ in front of a special character it makes sure that it is interpreted as a sign. Together with particular letters (\n oder \d) also for special signs.
  • \d This sign must be a digit.
  • \D This sign is allowed to be any sign except for a digit.
  • \w This sign must be of the following amount „0“ to „9“, „A“ to „Z“, „a“ to „z“ to „_“.

If you do not want special characters to be interpreted as control characters, but as normal signs, you must add „\“ as a prefix (in Java-String you have to input a double „\\“).

A little Java-example:

String str = "hello.txt";
System.out.println(str.matches("^hello\\..*"));  // true; corresponds to a "hello.*"


Detailed information (in engl.) at this page:
http://www.regular-expressions.info


Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007