sintassi
sintassi generica per le espressioni regalari prima di addottare iol metodo
private static final String REGEX = "espressione regolare"; stringa con l'espressione regolare da utilizzare
private static final String INPUT = "stringa"; stringa da analizzare
public static void main( String args[] )
{
Pattern p = Pattern.compile(REGEX); compilo l'espressione regolare}
Matcher m = p.matcher(INPUT); applico l'espressione regolare alla stringa
...
Espressione | Descrizione (inglese) |
^ | Matches the beginning of the line. |
$ | Matches the end of the line. |
. | Matches any single character except newline. Using m option allows it to match the newline as well. |
[...] | Matches any single character in brackets. |
[^...] | Matches any single character not in brackets. |
\A | Beginning of the entire string. |
\z | End of the entire string. |
\Z | End of the entire string except allowable final line terminator. |
re* | Matches 0 or more occurrences of the preceding expression. |
re+ | Matches 1 or more of the previous thing. |
re? | Matches 0 or 1 occurrence of the preceding expression. |
re{ n} | Matches exactly n number of occurrences of the preceding expression. |
re{ n,} | Matches n or more occurrences of the preceding expression. |
re{ n, m} | Matches at least n and at most m occurrences of the preceding expression. |
a| b | Matches either a or b. |
(re) | Groups regular expressions and remembers the matched text. |
(?: re) | Groups regular expressions without remembering the matched text. |
(?> re) | Matches the independent pattern without backtracking. |
\w | Matches the word characters. |
\W | Matches the nonword characters. |
\s | Matches the whitespace. Equivalent to [\t\n\r\f]. |
\S | Matches the nonwhitespace. |
\d | Matches the digits. Equivalent to [0-9]. |
\D | Matches the nondigits. |
\A | Matches the beginning of the string. |
\Z | Matches the end of the string. If a newline exists, it matches just before newline. |
\z | Matches the end of the string. |
\G | Matches the point where the last match finished. |
\n | Back-reference to capture group number "n". |
\b | Matches the word boundaries when outside the brackets. Matches the backspace (0x08) when inside the brackets. |
\B | Matches the nonword boundaries. |
\n, \t, etc. | Matches newlines, carriage returns, tabs, etc. (caratteri escape) |
\Q | Escape (quote) all characters up to \E. |
\E | Ends quoting begun with \Q. |
metodi
esempio: trova in sequenza inizio e fine substriga di cat quando parla intera non dentro altra parola
private static final String REGEX = "\\bcat\\b";
private static final String INPUT = "cat cat cat cattie cat";
public static void main( String args[] )
{
Pattern p = Pattern.compile(REGEX);}
Matcher m = p.matcher(INPUT);
int count = 0;
while(m.find()) metodo verifica
{count++;}
System.out.println("Match number "+count);
System.out.println("start(): "+m.start());
System.out.println("end(): "+m.end());
esempio
private static final String REGEX = "foo";
private static final String INPUT = "fooooooooooooooooo";
private static Pattern pattern;
private static Matcher matcher;
public static void main( String args[] )
{
pattern = Pattern.compile(REGEX);}
matcher = pattern.matcher(INPUT);
System.out.println("Current REGEX is: "+REGEX);
System.out.println("Current INPUT is: "+INPUT);
System.out.println("lookingAt(): "+matcher.lookingAt());
System.out.println("matches(): "+matcher.matches());
esempio
private static String REGEX = "dog";
private static String INPUT = "The dog says meow. " + "All dogs say meow.";
private static String REPLACE = "cat";
public static void main(String[] args)
{
Pattern p = Pattern.compile(REGEX);}
Matcher m = p.matcher(INPUT);
INPUT = m.replaceFirst(REPLACE);
System.out.println(INPUT);
INPUT = m.replaceAll(REPLACE);
System.out.println(INPUT);
esempio
private static String REGEX = "a*b";
private static String INPUT = "aabfooaabfooabfoob";
private static String REPLACE = "-";
public static void main(String[] args)
{
Pattern p = Pattern.compile(REGEX);}
Matcher m = p.matcher(INPUT);
StringBuffer sb = new StringBuffer();
while(m.find())
{m.appendReplacement(sb, REPLACE);}
m.appendTail(sb);
System.out.println(sb.toString());