Understanding java-11.4 Regular Expressions from the beginning (2)-Basic syntax

Source: Internet
Author: User
Tags character classes

In this section we will briefly describe the basic syntax of regular expressions.

Our goal is not to write the most complex expression, but to write an expression that completes the task.

1. Characters

Character Significance
\ t Tabs
\ n Line break
\ r Enter
\f Page change
\e Escape
\xhh Hexadecimal
\uhhhh Unicode hexadecimal

2. Character classes

Character class Significance
. Any character
[ABC] Any character that contains ABC
[^ABC] Any character that does not contain ABC
[A-za-z] A-Z of uppercase and lowercase characters
[Abc[ijk]] Same meaning as a|b|c|i|j|k (and set)
[A-z&&[ijk]] Characters that contain only Ijk (intersection)
\s whitespace (spaces, line breaks, page breaks, tab)
\s Non-whitespace character [^\s]
\d Digital
\d Non-digital
\w Word character [a-za-z0-9]
\w Non-word characters

3. Boundary characters

Symbol Significance
^ Begin
$ End
\b The boundary of the word
\b The boundaries of non-words
\g Last match end

4. Logic processing

Xy x followed by Y.
X| Y X or Y
X Capturing groups


5. Create a regular expression

Package Com.ray.ch11;public class Test {public static void main (string[] args) {string a = ' ddd012 '; for (string pattern: New string[] {"^[a-z]+[0-9]+$", "\\d+", "-?\\a+"}) {System.out.println (a.matches (pattern));}}}

Output:

True
False
False


Summary: This chapter mainly shows the simple syntax of regular expressions and how to create regular expressions in Java.


This chapter is here, thank you.

-----------------------------------

Directory



Understanding java-11.4 Regular Expressions from the beginning (2)-Basic syntax

Related Article

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.