Java -- Regular Expression-simply finding numbers in strings, java -- Regular Expression
import org.junit.Test;import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegexTest { @Test public void test(){ String line = "This order was placed for QT3000! OK?"; String pattern = "\\d+"; // Create a Pattern object Pattern r = Pattern.compile(pattern); // Now create matcher object. Matcher m = r.matcher(line); if (m.find( )) { System.out.println("Found value: " + m.group() ); } else { System.out.println("NO MATCH"); } }}
\ D + matches one or more numbers.
The output result is 3000.
The following is a regular expression metacharacter Syntax:
| Subexpression |
Matched |
| ^ |
Match the beginning of a line |
| $ |
Match the end of a row |
| . |
Match any single character except the linefeed. You can also use the m option to allow it to match the linefeed. |
| [...] |
Match any single character in the brackets. |
| [^...] |
Match any single character that is not in parentheses. |
| \ |
Start of the entire string |
| \ Z |
End of the entire string |
| \ Z |
The end of the entire string, except the end character of the last line |
| Re * |
Matches 0 or more pre-expression events |
| Re + |
Match one or more previous events |
| Re? |
Matches 0 or 1 items before expressing the event |
| Re {n} |
Match a specified n predicate events |
| Re {n ,} |
Match n or more pre-expression events |
| Re {n, m} |
Events are expressed before matching at least n m pieces |
| A | B |
Match a or B |
| (Re) |
Regular Expression group match text memory |
| (? : Re) |
No regular expression group matching text memory |
| (?> Re) |
Matching independent modes without backtracking |
| \ W |
Match word characters |
| \ W |
Match non-word characters |
| \ S |
Match spaces. Equivalent to [\ t \ n \ r \ f] |
| \ S |
Match non-space characters |
| \ D |
Match a number. It is equivalent to [0-9]. |
| \ D |
Match non-Numbers |
| \ |
Start of matching string |
| \ Z |
Matches the end of the string. If a new row exists, it matches the previous row. |
| \ Z |
Match the end of a string |
| \ G |
Match where the last match ends |
| \ N |
Return reference capture group number "N" |
| \ B |
Match the word boundary if it is not in parentheses. Match the backspace key in brackets |
| \ B |
Match non-word boundary |
| \ N, \ t, etc. |
Match line breaks, carriage returns, tabs, etc. |
| \ Q |
Start of the referenced character and end with \ E |
| \ E |
End reference starting with \ Q |