String processing in Java--Regular expressions

Source: Internet
Author: User

Recently in the processing of some payment messages, it is necessary to extract the required information from a variety of messages such as (amount, order number ...). ), each channel has a variety of messages, want to write a general extraction logic, and then review the use of Java regular expressions. Of course, we can write some algorithms ourselves, to extract these fields, but for the simple parsing, the hand-written algorithm is a bit overkill, and the regular expression can satisfy most of the character extraction needs. All right, gossip, not much. The common syntax for regular expressions is listed first:

    • \: A character that represents a special meaning for the next character, for example: \d match number
    • ^: The position that matches the beginning of the character, for example: ^#.*: Matches all characters beginning with #
    • $: Indicates the end position of the match character for example:. * Meta $: Matches characters ending in Meta
    • .: Indicates matching all characters
    • *: Indicates the number of occurrences is 0, or greater than 0 times
    • +: Indicates a number of occurrences of 1 or more than 1 times
    • ?: Indicates a match 0 or 1 times
    • {n}: the label matches the character exactly n times
    • {n,m}: Mark matches at least N times, up to M times
    • [XYZ]: Character set that matches any character in a character set, for example: [ABC] matches a in plan
    • [^xyz]: Inverse character set, equivalent to non-operation, representing characters not in the character set
    • (pattern): Sub-expression, in Java we take Group:matcher.group (0) from left to right;

Some commonly used special characters and their meanings are listed below:

    • \d: Match number, equivalent to [0-9]
    • \d: Match non-numeric, equivalent to [^0-9]
    • \ n: Match line break
    • \ r: Match carriage return character
    • \s: Matches a space or newline character or a page break, equivalent to [\f\n\r\t\v]
    • \s: matches any non-null character
    • \w: Matches any character, equivalent to [a-za-z0-9]
    • \w: Matches any non-word character

To deepen understanding and application, here are a few scenarios:

Take out the amount in the following string:

#支出合计: 33 of $10.00
String temp = "#支出合计: 33 pens, total $10.00";         = Pattern.compile ("(\\d+\\.\\d+)");         = P.matcher (temp);         if (Matcher.find ()) {            System.out.println (Matcher.group(0));            System.out.println (Matcher.group (1));        }

Output: 10.00

Discover the success of the message in the amount of recognition, so we only need to be different structure of the message containing the amount of the input, you can easily output the amount, simple Tiger balm ....

String processing in Java--Regular expressions

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.