Regular Expressions in Java

Source: Internet
Author: User

Common Regular Expression rules:

A: character

x character x. Example:' a ' denotes character a

\ \ backslash character.

\ n New Line (newline) character (' \u000a ')

\ r return character (' \u000d ')

B: character class

[ABC] a,b or C(Simple Class)

[^ABC] any character except a,B, or C(negation)

[A-za-z] A to z or a to z, the letters at both ends are included (range)

[0-9] 0 to 9 characters are included

C: Predefined character Classes

. any character. And mine is . the character itself, how to express it ? \.

\d Number:[0-9]

\w Word character:[a-za-z_0-9]

Things that make up words in regular expressions must have these things.

D: Boundary matching device

^ the beginning of the line

end of the $ line

\b Word boundaries

is the place where the word character is not.

Example:Hello World?haha;xixi

E:greedy number of words

X? X, not once or once

x* X, 0 or more times

x+ X, one or more times

X{n} X, exactly n times

X{n,} X, at least n times

X{n,m} X, at least n times, but no more than m Times

I'm like the next string : "91 27 46 38 50"

please write code to achieve the final output is:"27 38 46 50 9

Analysis:

A: define a string

B: Split the string to get an array of strings

C: transform the string array into an int array

D: sort int array

E: The sorted int array is assembled into a string

F: Output String

Demo:

public class Regextest {

public static void Main (string[] args) {

Define a string

String s = "91 27 46 38 50";

split the string to get an array of strings

string[] Strarray = S.split ("");

transform a string array into an int array

int[] arr = new Int[strarray.length];

for (int x = 0; x < arr.length; × x + +) {

ARR[X] = Integer.parseint (strarray[x]);

}

sort int array

Arrays.sort (arr);

The sorted int array is assembled into a string

StringBuilder sb = new StringBuilder ();

for (int x = 0; x < arr.length; × x + +) {

Sb.append (Arr[x]). Append ("");

}

Convert to String

String result = sb.tostring (). Trim ();

Output String

SYSTEM.OUT.PRINTLN ("Result:" +result);

}

}

The regular replacement function:

public string ReplaceAll of the String class (string regex,string replacement)

Replaces this string with the given replacement for all substrings that match the given regular expression.

Demo

public class Regexdemo {

public static void Main (string[] args) {

Define a string

String s = "Helloqq12345worldkh622112345678java";

I'm going to get rid of all the numbers and replace them with *

String regex = "\\d+";

String regex = "\\d";

String SS = "*";

kill the numbers directly .

String regex = "\\d+";

String ss = "";

String result = S.replaceall (regex, SS);

SYSTEM.OUT.PRINTLN (result);

}

}

Gets a three-character word in the following string

Da Jia Ting wo shuo,jin tian Yao xia yu,bu Shang wan zi Xi,gao xing bu?

Demo

public class RegexDemo2 {

public static void Main (string[] args) {

Defining Strings

String s = "da Jia ting wo shuo,jin tian Yao xia yu,bu Shang wan zi Xi,gao xing bu?";

rules

String regex = "\\b\\w{3}\\b";

compiling the rules into a schema object

Pattern p = pattern.compile (regex);

getting a Match object from a pattern object

Matcher m = P.matcher (s);

the ability to invoke a match object

The Find method is to look for substrings that have not met the criteria

public Boolean find ()

Boolean flag = M.find ();

SYSTEM.OUT.PRINTLN (flag);

How do I get a value ?

Public String Group ()

String ss = M.group ();

SYSTEM.OUT.PRINTLN (ss);

//

one more time.

Flag = M.find ();

SYSTEM.OUT.PRINTLN (flag);

SS = M.group ();

SYSTEM.OUT.PRINTLN (ss);

while (M.find ()) {

System.out.println (M.group ());

}

Note: Be sure to first find ( ) before group ()

Illegalstateexception:no Match found

String ss = M.group ();

SYSTEM.OUT.PRINTLN (ss);

}

}

Regular Expressions in Java

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.