Java regular expressions from entry to entry
If you have never used a regular expression, you may not be familiar with this term or concept. However, they are not as novel as you think.
Remember how to find files on the hard disk. Are you sure you want to use? And * characters to help find the file you are looking .? Character matches a single character in the file name, while * matches one or more characters. A file such as 'data ?. The following files can be found in the DAT mode:
Data1.dat
Data2.dat
Datax. dat
Datan. dat
If "*" is used instead? Characters to expand the number of files found. 'Data *. dat 'can match all the following file names:
Data. dat
Data1.dat
Data2.dat
Data12.dat
Datax. dat
Dataxyz. dat
Although this file search method is certainly very useful, it is also very limited .? The limited capabilities of wildcard and * enable you to define what a regular expression can do. However, regular expressions are more powerful and flexible.
-------------------------------------------------------
2. Early Origins
Early Origins
The "Ancestor" of regular expressions can be traced back to early studies on how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, developed a mathematical method to describe these neural networks.
In 1956, an American mathematician named Stephen Kleene published a paper titled "neural network event representation" based on McCulloch and Pitts's early work, introduces the concept of regular expressions. A regular expression is an expression used to describe the algebra of a positive set. Therefore, the regular expression is used.
Later, it was found that this work could be applied to some early research using Ken Thompson's computational search algorithm, which is the main inventor of UNIX. The first utility of regular expressions is the QED editor in UNIX.
As they said, the rest is the well-known history. Since then, regular expressions have been an important part of text-based editors and search tools.
--------------------------------------------------------
3. Use Regular Expressions
In typical search and replacement operations, you must provide the exact text to be searched. This technology may be sufficient for simple search and replacement tasks in static text, but it is difficult or even impossible to search dynamic text due to its lack of flexibility.
With a regular expression, you can:
1. Test the mode of a string. For example, you can test an input string to see if there is a phone number or a credit card number. This is called Data Validity verification.
2. Replace text. You can use a regular expression in a document to identify a specific text, and then delete it all or replace it with another text.
3. extract a substring from the string based on the pattern matching. It can be used to search for specific text in text or input fields.
For example, if you need to search the entire web site to delete outdated materials and replace some HTML formatting tags, you can use a regular expression to test each file, check whether there are materials or HTML formatting tags in the file. With this method, you can narrow down the affected files to the files that contain the materials to be deleted or changed. Then, you can use a regular expression to delete outdated materials. Finally, you can use a regular expression to find and replace the tags that need to be replaced.
Another example that describes the usefulness of regular expressions is a language with unknown string processing capabilities. VBScript is a subset of Visual Basic and has rich string processing functions. Visual Basic Scripting Edition similar to C does not have this capability. Regular Expressions significantly improve the string processing capability of Visual Basic Scripting Edition. However, it may be more efficient to use regular expressions in VBScript. It allows multiple string operations in a single expression.
4. Regular expression syntax
A regular expression is a text mode consisting of common characters (such as characters A to Z) and special characters (such as metacharacters. This mode describes one or more strings to be matched when searching the text subject. A regular expression is used as a template to match a character pattern with the searched string.
Here are some examples of regular expressions that may be encountered:
Visual basicvbscript matches Scripting Edition
/^/[/T] * $/CHR (34) ^/[/T] * $ CHR (34) matches a blank row.
// D {2}-/d {5}/CHR (34)/d {2}-/d {5} CHR (34) verify whether an ID number is composed of two characters, one
And a five-digit combination.
/<(. *)>. * <// 1>/CHR (34) <(. *)>. * <// 1> CHR (34) matches an HTML Tag.
The following table shows a complete list of metacharacters and their behaviors in the context of a regular expression:
Character Description
/Mark the next character as a special character, a literal character, a back reference, or an octal escape character. For example, 'n' matches the character CHR (34) NCHR (34 ). '/N' matches a line break. The sequence '//' matches CHR (34)/CHR (34), while CHR (34)/(CHR (34) matches CHR (34) (CHR (34 ).
^ Matches the start position of the input string. If the multiline attribute of the Regexp object is set, ^ matches the position after '/N' or'/R.
$ Matches the end position of the input string. If the multiline attribute of the Regexp object is set, $ also matches the location before '/N' or'/R.
* Matches the previous subexpression zero or multiple times. For example, Zo * can match CHR (34) zchr (34) and CHR (34) zoochr (34 ). * Is equivalent to {0 ,}.
+ Match the previous subexpression once or multiple times. For example, 'Zo + 'can match CHR (34) zochr (34) and CHR (34) zoochr (34), but cannot match CHR (34) zchr (34 ). + Is equivalent to {1 ,}.
? Match the previous subexpression zero or once. For example, CHR (34) Do (ES )? CHR (34) can match CHR (34) in CHR (34) dochr (34) or CHR (34) doeschr (34 ).? It is equivalent to {0, 1 }.
{N} n is a non-negative integer. Match n times. For example, 'O {2} 'cannot match
'O' in CHR (34) bobchr (34), but can match two o in CHR (34) foodchr (34.
{N,} n is a non-negative integer. Match at least N times. For example, 'O {2,} 'cannot match 'O' in CHR (34) bobchr (34), but can match all o in CHR (34) fooooodchr (34. 'O {1,} 'is equivalent to 'o + '. 'O {0,} 'is equivalent to 'o *'.
Both {n, m} m and n are non-negative integers, where n <= m. Match at least N times and at most m times. Liu, CHR (34) o {1, 3} CHR (34) will match the first three o in CHR (34) foooodchr (34. 'O {0, 1} 'is equivalent to 'o? '. Note that there must be no space between a comma and two numbers.
? When this character is followed by any other delimiter (*, + ,?, The matching mode after {n}, {n ,}, {n, m}) is not greedy. The non-Greedy mode matches as few searched strings as possible, while the default greedy mode matches as many searched strings as possible. For example, for the string CHR (34) oooooochr (34), 'O ++? 'Will match a single CHR (34) ochr (34), while 'o +' will match all 'O '.
. Match any single character except CHR (34)/NCHR (34. To match any character including '/N', use a pattern like' [./N.
(Pattern) matches pattern and obtains this match. The obtained match can be obtained from the generated matches set. The submatches set is used in VBScript, and $0… is used in Visual Basic Scripting Edition... $9 attribute. To match the parentheses, use '/(' or '/)'.
(? : Pattern) matches pattern but does not get the matching result. That is to say, this is a non-get match and is not stored for future use. This is useful when using the CHR (34) or CHR (34) character (|) to combine various parts of a pattern. For example, 'industr (? : Y | ies) is a simpler expression than 'industry | industries.
(? = Pattern) Forward pre-query: matches the search string at the beginning of any string that matches pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (? = 95 | 98 | nt | 2000) 'can match CHR (34) windowschr (34) in Windows 2000chr (34), but cannot match CHR (34) windows3. CHR (34) windowschr (34) in 1chr (34 ).
Pre-query does not consume characters, that is, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
(?! Pattern) negative pre-query, match the search string at the start of any string that does not match negative lookahead matches the search string at any point where a string not matching pattern. This is a non-get match, that is, the match does not need to be obtained for future use. For example, 'windows (?! 95 | 98 | nt | 2000) 'can match CHR (34) windowschr (34) in Windows 3.1chr (34), but cannot match CHR (34) CHR (34) windowschr (34) in Windows 2000chr (34 ). Pre-query does not consume characters. That is to say, after a match occurs, the next matching search starts immediately after the last match, instead of starting after the pre-query characters.
X | y matches X or Y. For example, 'z | food' can match CHR (34) zchr (34) or CHR (34) foodchr (34 ). '(Z | f) Ood' matches CHR (34) zoodchr (34) or CHR (34) foodchr (34 ).
[Xyz] Character Set combination. Match any character in it. For example, '[ABC]' can match 'A' in CHR (34) plainchr (34 '.
[^ XYZ] combination of negative character sets. Match any character not included. For example, '[^ ABC]' can match 'p' in CHR (34) plainchr (34 '.
[A-Z] character range. Matches any character in the specified range. For example, '[A-Z]' can match any lowercase letter in the range of 'A' to 'Z.
[^ A-Z] negative character range. Matches any character that is not within the specified range. For example, '[^ A-Z]' can match any character that is not in the range of 'A' to 'Z.
/B matches a word boundary, that is, the position between a word and a space. For example, 'er/B 'can match 'er' in CHR (34) neverchr (34), but cannot match 'er 'in CHR (34) verbchr (34 '.
/B matches non-word boundaries. 'Er/B 'can match 'er in CHR (34) verbchr (34)