The background of Java regular Expressions __ regular expressions

Source: Internet
Author: User
Tags alphabetic character chr

Java Regular Expressions

If you had not used regular expressions, you might not be familiar with this term or concept. However, they are not as novel as you might think.
  
Recall how you found the file on your hard disk. Are you sure you will use it? and * characters to help find the file you are looking for. The character matches a single character in the file name, while the * matches one or more characters. One like ' data?. DAT ' mode can be found in the following files:
  
Data1.dat
  
Data2.dat
  
Datax.dat
  
DataN.dat
  
What if you use the * character instead? Character, the number of files found will be enlarged. ' Data*.dat ' can match all of the following file names:
  
Data.dat
  
Data1.dat
  
Data2.dat
  
Data12.dat
  
Datax.dat
  
DataXYZ.dat
  
Although this method of searching for files is certainly useful, it is very limited. and * The limited ability of wildcards allows you to have a concept of what regular expressions can do, but regular expressions are more powerful and flexible.
  
-------------------------------------------------------
2. Early origins
  
Early origins
  
The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, have developed a mathematical way of describing these neural networks.
  
In 1956, an American mathematician named Stephen Kleene, based on the early work of McCulloch and Pitts, published a paper entitled "Representation of neural network events", introducing the concept of regular expressions. A regular expression is an expression that describes what he calls the algebra of a regular set, so the term "regular expression" is used.
  
Subsequently, it was found that this work could be applied to some early studies using Ken Thompson's computational Search algorithm, and Ken Thompson was the main inventor of Unix. The first practical application of regular expressions is the QED editor in Unix.
  
As they say, the rest is a well-known history. From then until now regular expressions are an important part of text-based editors and search tools.
--------------------------------------------------------
3. Using regular expressions
  
In a typical search and replace operation, you must provide the exact text you want to find. This technique may be sufficient for simple search and replace tasks in static text, but because of its lack of flexibility, it is difficult or even impossible to search for dynamic text.
  
Using regular expressions, you can:
  
1. Test a pattern for a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is known as data validation.
  
2. Replace text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
  
3. Extracts a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.
  
For example, if you need to search the entire Web site to remove some outdated material and replace some HTML formatting tags, you can use regular expressions to test each file to see if there are any material or HTML formatting tags that you want to find in the file. With this method, you can narrow the affected file range to those files that contain the material you want to delete or change. You can then use regular expressions to delete obsolete materials, and finally, you can use regular expressions again to find and replace those that need to be replaced.
  
Another example that illustrates the usefulness of regular expressions is a language whose string-handling power is not yet known. VBScript is a subset of Visual Basic that has rich string processing capabilities. Visual Basic scripting Edition similar to C does not have this capability. Regular expressions have significantly improved the ability of the Visual Basic scripting Edition to handle string processing. However, it may still be more efficient to use regular expressions in VBScript, which promises to perform multiple string operations in a single expression
  
4. Regular expression syntax
A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and extraordinary characters (called metacharacters). This pattern describes one or more strings to be matched when looking for a text body. A regular expression is used as a template to match a character pattern with the string being searched for.
  
Here are some examples of regular expressions that you might encounter:
  
Visual basicvbscript matching scripting Edition
  
/^/[/T]*$/CHR (^/[)/t]* $CHR (34) matches a blank line.
  
D{2}-/D{5}/CHR (/D{2}-/D{5}CHR) (34) verifies that an ID number is a 2-digit word, a
A hyphen and a 5-digit number.
  
/< (. *) &GT;.*&LT;///1&GT;/CHR () < (. *) &GT;.*&LT;///1&GT;CHR (34) matches an HTML tag.
  
The following table is a complete list of metacharacters and its behavior in the context of regular expressions:
  
Character description
  
/Mark the next character as an extraordinary character, or a literal character, or a back reference, or a octal escape character. For example, the ' n ' matching character chr (34) NCHR. ' n ' matches a newline character. The sequence '//' matched Chr (34)/CHR (34) and Chr (34) matched Chr (CHR).
  
^ matches the start position of the input string. If the Multiline property of the RegEXP object is set, ^ also matches the position after ' n ' or ' R '.
  
$ matches the end position of the input string. If the multiline property of the RegExp object is set, the $ also matches the position before ' n ' or ' R '.
  
* Match the preceding subexpression 0 or more times. For example, zo* can match Chr (34) and Chr (ZCHR) ZOOCHR (34). * is equivalent to {0,}.
  
+ matches the preceding subexpression one or more times. For example, ' zo+ ' can match CHR ZOCHR (34) and Chr (34), but cannot match ZOOCHR (34). + is equivalent to {1,}.
  
? Match the preceding subexpression 0 times or once. For example, Chr do (es) Chr (34) can match Chr (34) in Chr (34) DOESCHR (34) or Chr (DOCHR). is equivalent to {0,1}.
  
{N}n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match
' O ' in Chr (34), but can match two o in Chr (34) foodchr.
  
{N,}n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in Chr BOBCHR (34) but can match all o in Chr (a) FOOOOODCHR (34). ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
  
{N,m}m and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. Liu, Chr (O{1,3}CHR) (34) will match the first three O in Chr (FOOOOOODCHR) (34). ' o{0,1} ' is equivalent to ' o '. Please note that there should be no space between commas and two numbers
  
? When the character is immediately following any of the other qualifiers (*, +,?, {n}, {n,},{n,m}), the matching pattern is not greedy. Non-greedy patterns match as few strings as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for string Chr (OOOOCHR), ' o+? ' will match a single CHR OCHR (34), and ' o+ ' will match all ' o '.
  
. matches any single character except Chr (34)/nchr. To match any character including '/n ', use a pattern like ' [. N] '.
  
(pattern) matches the pattern and gets the match. The obtained matches can be obtained from the resulting matches collection, use the Submatches collection in VBScript, and use the $0...$9 property in visual Basic scripting Edition. To match the parentheses character, use '/(' or '/'.
  
(?:p Attern) matches pattern but does not get matching results, which means that this is a non fetch match and is not stored for later use. This is useful for combining parts of a pattern using Chr (34) or Chr (34) characters (). For example, ' Industr (?: yies) is a more abbreviated expression than ' industryindustries '.
  
(? =pattern) forward lookup, matching the find string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' windows (? =9598nt2000) ' Can match Chr (34) in CHR Windows 2000CHR (34), but cannot match Chr WINDOWS3.1CHR (34) Chr (WINDOWSCHR) (34).
It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.
  
(?! pattern) Negative pre-check, matching at the beginning of any mismatched negative lookahead matches the search string at any point where a string does matching pattern Find the string. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Windows (?! 9598nt2000) ' Can match Chr (34) in Windows 3.1CHR (34), but cannot match Chr (34) in CHR Windows 2000CHR (34) WINDOWSCHR (34). It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.
  
XY matches x or Y. For example, ' Zfood ' can match CHR ZCHR (34) or Chr (FOODCHR) (34). ' (ZF) Ood ' matches Chr ZOODCHR (34) or Chr (FOODCHR) (34).
  
[XYZ] Character set combination. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in Chr (34) plainchr.
  
[^XYZ] Negative character set combination. Matches any characters that are not included. For example, ' [^ABC] ' can match ' P ' in Chr PLAINCHR (34).
  
[A-z] character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the range ' a ' to ' Z '.
  
[^a-z] a negative character range. Matches any character that is not in 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, which means the position between the word and the space. For example, ' er/b ' can match ' er ' in Chr neverchr (34), but it does not match ' er ' in Chr verbchr (34).
  
/b matches a non word boundary. ' er/b ' can match ' er in Chr ' VERBCHR (34)

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.