In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the regular expressions in C, Java, JavaScript, which have a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
What is a regular expression?
A regular expression (Regular Expression) is a formula that uses a pattern to match a class of strings. If you want to find the name of three words in an article where the first word is "Luo" and the last word is "Hao", that is, "Luo * Hao", then "Luo * Hao" is the formula, also known as Pattern, and this article is the string to match (or text text). For example, if you want to check whether a string entered is in the format of 126 mailbox, you have to make a rule to check it, which is a regular expression.
Start from the beginning
Let's start with an example mentioned above: check whether a string conforms to the format of the 126 mailbox.
From NetEase's mailbox registration page, we can see that the user name of the 126mailbox needs to be in the following format: 61characters, you can use letters, numbers, underscores, and start with a letter. We can define a pattern: ^ [a-zA-Z]\ w {5pm 17} @ 126.com
This pattern can be understood as follows:
[a-zA-Z]: any letter a to z or A to Z
^: indicates what to start with, then ^ [a-zA-Z] means to start with a letter
\ w: word character [a-zA-Z_0-9], that is, any character in Amurz or Amurz or 0-9 or _
{5pm 17}: indicates that there are 5 to 17 occurrences (at least 5 times, no more than 17 times), then\ w {5pm 17} indicates 5 characters.
Because there is also a character that begins with a non-numeric letter, ^ [a-zA-Z]\ w {5pm 17} means: "618 characters, you can use letters, numbers, underscores, and you need to start with a letter."
@ 126.com: indicates that the user name that meets the above rules is followed by the @ 126.com string to form an email address.
"^ [a-zA-Z]\ w {5 a-zA-Z 17} @ 126.com" is what we call a regular expression, which is simply implemented in Java as follows:
String regex = "^ [a-zA-Z]\\ w {5pm 17} @ 126\\ .com"; / / define matching rules: regular expressions
/ / description: in 126.com. Need to escape.
String text = "ZhanSan@126fcom"; / / string to check
Boolean isMatched = text.matches (regex); / / determine whether text conforms to the rule regex
System.out.println (isMatched)
Common symbols for regular expressions
The "^", "\ w" and "{5jin17}" used in the above example are all common symbols in regular expressions, all of which have special meanings in regular expressions. The following table is the meaning of the symbols commonly used in regular expressions in Java (only the commonly used parts are extracted to illustrate, which can actually solve most of the problems about regular expressions).
Pattern
Matching content (meaning)
Character class
[abc]
A, b or c (simple class)
[^ abc]
Any character except a, b, or c (negative)
[a-zA-Z]
A to z or A to Z, with letters at both ends included (range)
[amurd [mmurp]]
A to d or m to p: [a-dm-p] (union)
[a Methodist & [def]]
D, e or f (intersection)
[a Methodist & [^ BC]]
A to z, except for b and c: [ad-z] (minus)
[a Mustang & [^ mmurp]]
A to z, not m to p: [a-lq-z] (minus)
Predefined character class
.
Any character (may or may not match the line Terminator)
\ d
Number: [0-9]
\ d
Non-numeric: [^ 0-9]
\ s
White space character: [\ t\ n\ x0B\ f\ r]
\ s
Non-white space character: [^\ s]
\ w
Word character: [a-zA-Z_0-9]
\ W
Non-word characters: [^\ w]
Boundary matcher
^
The beginning of a line
$
The end of the line
\ b
Word boundary
\ B
Non-word boundary
\ a
The beginning of the input
\ G
The end of the last match
\ Z
The end of the input, only for the last Terminator (if any)
\ z
The end of the input
Quantifier (Greedy strategy)
X?
X, once or not once.
X*
X, zero 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 not more than m times
Logical operator
XY
X followed by Y
X | Y
X or Y
(X)
X, as a capture group
These commonly used symbols have basically the same meaning in regular expressions in various programming languages (because the idea of regular expressions is the same), so they can be used as parameters. However, there may be some small differences between different languages, and for more accurate and authoritative instructions for various programming languages, please refer to their official documentation:
C++ (VS2013 compiler): http://msdn.microsoft.com/zh-cn/library/bb982727.aspx#grammarsummary
Java: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
JavaScript: http://www.w3school.com.cn/jsref/jsref_obj_regexp.asp
The use of regular expressions
The following describes the use of regular expressions in C++, Java, and JavaScript based on common requirements
Regular expressions in C++
There are three main ways to implement regular expressions in C++: the implementation of C language (C regex), the implementation of C++ standard library (C++ regex) and the implementation of Boost library (boost regex). C regex is a process-oriented programming method, which is not very convenient to use. C++ regex can be used directly because it is part of the standard library (as if it is not supported on the Linux platform), but C++ regex is very difficult to use, syntax requirements are relatively strict, and many default options are different from our normal thinking. Boost is an open source third-party library, this library is very excellent, widely used in C++ project development, boost regex is very flexible and easy to use, C++ development is a highly respected way.
About the usage of boost regex, which will be further introduced in the following article, now give a use example in the way of C++ regex.
1. Verify ip addr
# include # include # include bool IsIpV4Address (const std::string& strIp) {/ / verify the mode of the IP address, where "\." The "\" in is an escape character, indicating that this is a. Const std::regex pattern ("(\\ d {1Power3}) {1}\. (\\ d {1Power3}) {1}"); / / matching verification return std::regex_match (strIp, pattern);} int main () {std::string strIp1 = "134.34.34.4"; / 192.168.1.1 std::string strIp2 = "192.168.255"; std::cout
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.