Java -- Regular Expression-simply finding numbers in strings, java -- Regular Expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.