| Start | ||||||||
|---|---|---|---|---|---|---|---|---|
| Using capture groups If you need to extract a part of string from the input string, we can use capture groups of regex. For this example, we'll start with a simple phone number regex: If parentheses are added to the regex, each set of parentheses is considered a capturing group. In this case, we are using what are called numbered capture groups: Before we can use it in Java, we must not forget to follow the rules of Strings, escaping the backslashes, resulting in the following pattern: We first need to compile the regex pattern to make a Next, the Matcher needs to find the first subsequence that matches the regex: Now, using the group method, we can extract the data from the string: Note: Java 7 introduced named capture groups. Named capture groups function the same as numbered capture groups (but with a name instead of a number), although there are slight syntax changes. Using named capture groups improves readability. We can alter the above code to use named groups: To get the contents of "AreaCode", we can instead use: |
||||||||
| Using regex with custom behaviour by compiling the Pattern with flags A |
||||||||
| Escape Characters Generally To use regular expression specific characters ( These two examples will not work: This example works Splitting a Pipe Delimited String This does not return the expected result: This returns the expected result: Escaping backslash This will give an error: This works: |
||||||||
| Not matching a given string To match something that does not contain a given string, one can use negative lookahead: Regex syntax: Example: Output: |
||||||||
| Matching with a regex literal. If you need to match characters that are a part of the regular expression syntax you can mark all or part of the pattern as a regex literal.
An easier way of doing it without having to remember the |
||||||||
| Matching a backslash If you want to match a backslash in your regular expression, you'll have to escape it. Backslash is an escape character in regular expressions. You can use '\' to refer to a single backslash in a regular expression. However, backslash is also an escape character in Java literal strings. To make a regular expression from a string literal, you have to escape each of its backslashes. In a string literal '\\' can be used to create a regular expression with '\', which in turn can match ''. For example, consider matching strings like "C:\dir\myfile.txt". A regular expression To express that pattern in a Java string literal, each of the backslashes in the regular expression needs to be escaped. If you want to match two backslashes, you'll find yourself using eight in a literal string, to represent four in the regular expression, to match two. Syntax
RemarksImportsYou will need to add the following imports before you can use Regex: PitfallsIn java, a backslash is escaped with a double backslash, so a backslash in the regex string should be inputted as a double backslash. If you need to escape a double backslash (to match a single backslash with the regex, you need to input it as a quadruple backslash. Important Symbols Explained
Further readingThe regex topic (opens new window) contains more information about regular expressions. |
Read Article
https://develop.horje.com/learn/1434/reference