Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use scala regular expressions in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How can scala regular expressions be used in Java? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Scala supports regular expressions through the Regex class in the scala.util.matching package.

Eg: using regular expressions to find the word Scala

An Regex object is constructed using the r () method of the String class in the instance. (you can also new an instance of a Regex object directly) and then use the findFirstIn method to find the first match. You can use the findAllIn method if you need to see all the matches.

You can use the mkString () method to concatenate strings of regular expression matching results, and you can use pipes (|) to set different modes:

If you need to replace the matching text with the specified keyword, you can use the replaceFirstIn () method to replace the first match and the replaceAllIn () method to replace all matches

Regular expression

Scala's regular expressions inherit the syntax rules of Java, while Java mostly uses the rules of the Perl language.

The following table shows some common regular expression rules:

The grammar rules of Java and Java mostly use the rules of Perl language.

The following table shows some common regular expression rules:

Expression matching rule

^ matches the position where the input string begins.

$matches the position at the end of the input string.

. Matches any single character except "\ r\ n".

[...] Character set. Matches any character contained. For example, "[abc]" matches "a" in "plain".

[^.] Reverse character set. Matches any characters that are not included. For example, "[^ abc]" matches "p", "l", "I", "n" in "plain".

\ A matches the position where the input string begins (no multiline support)

The end of the\ z string (similar to $, but not affected by the multiline option)

\ Z end of string or end of line (not affected by handling multiline options)

Re* repeats zero or more times

Re+ is repeated one or more times

Re? Repeat zero or once

Re {n} repeat n times

Re {n,}

Re {n, m} repeat n to m times

A | b matches an or b

(re) matches the re and captures the text to the automatically named group

(?: re) matches re, does not capture matching text, and does not assign a group number to this packet

(? > re) greedy subexpression

\ w match letters or numbers or underscores or Chinese characters

\ W matches any character that is not letters, numbers, underscores, or Chinese characters

\\ s matches any space character, which is equal to [\ t\ n\ r\ f]

\ S matches any character that is not a blank character

\\ d match numbers, similar to [0-9]

\ D matches any non-numeric character

\ G the beginning of the current search

\ nnewline character

\ b is usually the word demarcation position, but if used in a character class to represent backspace

\ B match is not the beginning or end of a word.

\ t tabs

\ Q starts with quotation marks:\ Q (aqb) * 3\ E matches the text "(aqb) * 3".

\ E closing quotation marks:\ Q (aqb) * 3\ E matches the text "(aqb) * 3".

Regular expression instance

Case description

. Matches any single character except "\ r\ n".

[Rr] uby matches "Ruby" or "ruby"

Rub [ye] matches "ruby" or "rube"

[aeiou] matches lowercase letters: aeiou

[0-9] matches any number, similar to [0123456789]

[amurz] matches any ASCII lowercase letter

[Amurz] matches any ASCII capital letter

[a-zA-Z0-9] match numbers, uppercase and lowercase letters

[^ aeiou] matches other characters except aeiou

[^ 0-9] matches characters other than numbers

\\ d match numbers, similar to: [0-9]

\\ D matches non-numeric, similar to: [^ 0-9]

\ s match spaces, similar to: [\ t\ r\ n\ f]

\ s matches non-spaces, similar to: [^\ t\ r\ n\ f]

\\ w matches letters, numbers, underscores, similar to: [A-Za-z0-9 _]

\\ W matches non-letters, numbers, underscores, like: [^ A-Za-z0-9 _]

Ruby? Matching "rub" or "ruby": y is optional

Ruby* matches "rub" plus 0 or more y.

Ruby+ matches "rub" plus one or more y.

\\ d {3} exactly matches 3 digits.

\\ d {3,} matches 3 or more digits.

\\ d {3re5} matches 3, 4, or 5 numbers.

\\ D\\ d + No grouping: + repeat\ d

(\\ D\\ d) + / grouping: + repeat\ D\ d pair

([Rr] uby (,)?) + matches "Ruby", "Ruby, ruby, ruby", etc.

-

Reference: http://www.runoob.com/scala/scala-regular-expressions.html

Note: each character in the above table uses two backslashes. This is because the backslash in the string in Java and Scala is an escape character. So to output.\. You need to write it as.\ in the string. To get a backslash.

Eg:

After reading the above, have you mastered how scala regular expressions are used in Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report