| Purpose | Input | Requirements | Regular Expression |
|---|---|---|---|
| Checking | Markus | Is it a valid username? Thus no special characters, maybe numbers and not empty. | [a-zA-Z0-9]+ |
| Extracting | Product=BigMac, Price=2,50 | Extract the product name and the price | Product=([^,]+), Price=([0-9]+),([0-9]+) |
00: import java.util.regex.Pattern;
01: import java.util.regex.Matcher;
02:
03: public class SomeClass
04: {
05: public static void main(String[] args)
06: {
07: String line = "Product=BigMac, Price=2,50";
08:
09: Pattern p = Pattern.compile("Product=([^,]+), Price=([0-9]+),([0-9]+)"); // create the pattern only once, it can't change!
10:
11: Matcher m = p.matcher(line);
12:
13: if(m.find()) // use while(m.find) if the pattern can occur more than once per line
14: {
15: String prodName = m.group(1); // capturing group number 1
16: String priceStr = m.group(2) + "." + m.group(3);
17: double price = Double.parseDouble(priceStr);
18:
19: // use the extracted information...
20: System.out.println(prodName);
21: System.out.println(price);
22: }
23: }
24:}
| Input | Product[BigMac] Price[2,50] |
| Pattern | Price\[[0-9]+\] |
| Java | Pattern p = Pattern.compile("Price\\[[0-9]+\\]"); |
| Price\[ | The pattern searches for Price[. The square bracket [ must be escaped, as it is a special character. |
| [0-9]+ | Matches 1 or more numbers |
| \] | Matches a square bracket ]. |
| Input | <word conf="1.0" end="1234">That</word> |
| Pattern | <word .* end="([0-9]+)" |
| Java | Pattern p = Pattern.compile("<word .* end=\"([0-9]+)\""); |
| <word [ | The pattern searches for <word. |
| .* | Matches 0 or more characters (any) is matched. Note: the .* pattern should be used carefully, thus use it only once per pattern. |
| end=" | Matches the text end=". The double quotes must be escaped in the Java string as it would close the string. |
| ([0-9]+) | Introduces a new capturing group, containing 1 or more numbers. |
| Input | a=123 b=456 | <word>Your phone number is 123</word> |
| Pattern | [ab]=([0-9]+) | <word>([^<]+)</word> |
| Java | Pattern p = Pattern.compile("[ab]=([0-9]+)"); | Pattern p = Pattern.compile("<word>([^<]+)</word>"); |
| [ab] | A single a or a single b. |
| ([0-9]+) | Introduces a new capturing group, containing 1 or more numbers. |
| ([^<]+) | Introduces a new capturing group, containing 1 or more characters except <. |
| ? | 0 or 1 occurence. Thus the element before would be optional. |
| + | 1 or more occurences. Thus the element before must occur at least once. |
| * | 0 or more occurences. |
| {N} | The element before must occur exactly N times. |