Regular expression example

xiaoxiao2021-03-06  44

[1] © Monitan BBS Network Community-Monita BBS network community; a regular operation is a specific format mode that can be used to find a string in another string. Several programming languages, including Visual Basic, Perl, JavaScript, and PHP support the regular operation, I hope to end in this entry guidance, Mitch # ll (authors yourself) allows you to apply some basic regular rules in the PHP program formula. The regular operation is a kind of strange feature in a variety of prior clauses, but because they seem to have a difficult concept, many developers put them in the corner and forgot their existence. BCR, 5 Let's take a look at what is a regular operation, why you want to use them in a PHP program. TP What is a regular arithmetic model? LR = y You have some views from a good old-based text editor, what is the view like BbEdit and notepad? Both support text input, allow you to save text to the file, but now the text editor also supports other features, including find-instead of tools, which makes editing a text file is quite easy. # Regular models is similar, just better. Regular operations can be considered an extremely advanced lookup - replacement tool, let us get rid of pain from pain: no need to write custom information confirmation examples to check the email address or to confirm the format of the phone number is correct, so wait Wait. One of the most common functions in OUI is the data effectiveness check. PHP is bundled with some text check functions, allowing us to match a string with a regular operation, confirm that there is a space, and a question mark, and so on. N {You don't know, can the regular operation can be simply equipped, when you master some regular arithmeters (this regular arithmetic can be used to tell the regular calculator engine one of the parties we want to match) You will ask why you will throw the regular arithmetic to throw it in the corner for so long, ^ _ ^. Y) PHP has two sets of functions to handle two types of regular operations: Perl5 compatible mode, and POSIX standard compatibility mode. In this article we will look at the EREG function and use the search operation of the POSIX standard. Although they do not have the PERL5 mode, a good way to learn the regular arithmetic method. If you are interested in the PERL5 of PHP support, you can find some details about the preg function to the PHP.NET website. } ZC6R PHP has six functions to handle regular operations. They all use a regular operation as their first parameters, listed as follows: Z • EREG: The most commonly used regular operational function, EREG allows us to search with one A string of regular operations match. / • EREG_REPLACE: Allows us to search for a string matching the regular arithmetic compatible, and replaced all this arithmetic place with a new string. 2zxw • EREGI: and EREG is almost the same effect, but ignore the case. D-OS • EREGI_REPLACE: and EREG_REPLACE have the same search - replacement, but the case is negligible. T • split: Allows us to search and regular operational calculating strings, and return match results in a string collection. P ^ X • Spliti: Split function ignores the case of cases. Why is Y4T to use a regular arithmetic calculation? If you constantly establish a different function to check or operate a string, now you may want to give up all these functions, replaced by Regular operation.

If you answer the following questions, "Yes", then you must consider using the regular arithmetic calculation: JZ% # vd • Are you writing some custom functions to check form data (such as in the electronic mailbox site One @, a point)? T '$ V} • Do you write some custom functions, loose a circle in a string, if this character matches a specific feature (for example it is capitalized, or It is a space), then replace it? & 5]} PI In addition to being uncomfortable string check and operation method, if there is no efficiency to write code, the above two will slow your program slowly. Are you more preferred to check an electronic mail address with the following code: J n else E] Iafn Return False; RJ / G $} © Mimony BBS Network Community - Monitric BBS Network Community. A7echo ValidateEmail ("mitch#ll@devarticles.com"); 4C?> kix. Or Use the following code:]) xwe} © Moni BBS Network Community - Monita BBS Network Community I {6gmecho ValidateEmail ("Mitch#ll@devarticles.com"); 0pec ~?> 1 (4ax can be sure, the first function is relatively easy, and it looks good. But if we use the next version of the Email Is the site check function not easier?] The second function above display only the regular arithmetic, including a call for EREG functions. EREG function returns true or false to declare whether its string parameters are comparable to regular operation Matching. C4F | Many programmers avoid the regular arithmetic, just because they (in some cases) is slower than other text processing methods. The rules of the rules can be slow because they involve the strings in memory Body copy and paste, because each new part of the regular arithmetic correspondence is matched with a string. However, from my experience of the regular operation, unless you run a complex regular regular in the text. The calculation, otherwise defects can be ignored. When the regular operation is used as the input data inspection tool, this situation is rare. Ycx © Moni BBS Network Community - Monita BBS Network Community TOM: X Regular Computational Syntax BR / Before you can match a string to the regular operation, you must first establish a regular arithmetic. At the beginning, the syntax of the rules of the rules is a bit weird, and each phrase in the arithmetic represents a type of search characteristics.

The following is some of the most common regular operations, and there should be an example of how to use it:> o0} [string head 1 Search a string head, use ^, for example (EP8 = 9 will return True, but {"-, c & (] E will return false, because h # llo is not in the header of the string" I Say H # llo world ". HU0; string tail; z6 search word tail, with $, for example: 2 & 'I $ n8! & Pd will return TRUE, but B > W ?? o will return false, because Bye is not in the tail of the string "Goodbye My Friend". II arbitrary single character i search for any font, point (.), for example: 8 Q! m will return True, but 9_Q GNP will return false, because our search string does not contain a word You can tell the regular arc arc to tell the regular arithmetic engine to match how many single characters. If I just want to match 5 characters, I can use EREG: B> YIGV ~ D3B This code tells the regular arithmetic engine when and only when at least 5 consecutive characters appear on the tail of the string. We can also limit the continuous display Number: ABFHJ K In the above example, we have told the regular arithmetic engine, our search strings come to match the calculation, it must be on the tail With an internal to 1 and 3 "A" character. V X / I % [ED The above example will not return true, though There are three "A" characters in searching, but they are not the tail of the strings. If we match the end string to the end of the regular arithmetic, then this string is matched. @ S-6R ' We can also tell the regular arithmetic engine to match at least a certain number of characters in one line, if they exist, can match more. We can do this: <)] z "< PHP Echo EREG ("a $", "aaaa");?> 9Q_ [2] © Mony BBS Network Community - Monita BBS Network Community 5C / YS Zero or Multiple Repeat Characters 7 In order to tell the regular The arithmetic engine may exist, you can repeat, we use * vol. The two examples here will return true. | % "Y9 > J} A85 does not contain "T" by the second example, but still returns Ture, because * indicates that the character can appear, but it is not necessary.

In fact, any normal string mode returns True above the EREG call because 't' character is optional. DH one or more repeat character%:! O In order to tell the regular arithmetic engine a word Must exist, you can also repeat more than once, we use vol, like n; hr R = jt. The example below will return true. : FH] / Zero or a repetitive character nj We can also tell the regular arithmetic engine, a character must or only once Or no. We use the character to do this, like [/%, 5 If we prefer, we can search from above. Delete 'C' in the string, this arithmeter still returns True. '?' The meaning is that a 'c' can appear anywhere in the search string, but it is not necessary. G: 0ez Regular Computational Syntax (contd.) K Space By BPFW In order to match a space in a search string, we use the predefined POSIX class, [[: space]]. Square brackets indicate that the continuous font Relevance, ": Space:" is a class that actually matches (in this case, it is any blank character). Blank includes Tab character, new line fifth, blank font. Or, if the search string must contain only one space, instead of a Tab or a new line, you can use a spaceblock (""). In most cases, I tend to use ": Space:", because this means that my intent is not just a single space character, this is easy to be ignored. Here are some POSIX-standard predefined classes, 9Z6O 'has some POSIX-standard predefined classes we can use as a regular arithmetic part, including [: alnum:], [: Digit:], [: Lower:], etc. Wait. Complete list You can view P1kr here> We can match a single blank font: ^ 1 q We can also tell the regular arithmetic engine to match without blank or a blank by using the character. 2.mc ~ [X8 | 5. Mode Group h related mode can be in square brackets Some together. It is easy to specify only one lowercase letter or a column of uppercase letters with [A-Z] and [A-Z] to search for some part of the search string. '.prpa ?> `D ) & Or like> WPCB (* NR We can also tell the regular arithmetic engine, we hope or lowercase letters, or uppercase letters. We can do it together with [AZ] and [AZ] mode.

CZ { - / ~ In the above example, if we can match "john doe", not "Johndoe ", Will be very meaningful. We use the following regular operations to do this: uao "; * ^ [A-ZA-Z] [[: space:]] [A-ZA-Z] $ WQFQVQ is easy to search for a digital string ') G> L , fp word group K Not only search mode, we can also use parentheses to search words Packet. 7 & ) V; f. In the above example, we have a string header word Yuan, close to "john" or "jane", at least one other character, then a string tail niece. So ... 1 BW0" ... will also match our search mode F special vollavis, 2ysa5, because some characters are used in a clear group or syntax in a search mode, like (John | Jane) In the parentheses, we need to tell the regular arithmetic engine to mask these numerals, processes them into part of the search string, rather than searching for a selection. The method we use is called "character escape", involving any "dedicated symbol" plus a backslash. So, for example, if I want to include '|', then I can do this GX8M SB is just a small amount of some of the characters you want to escape, you must escape ^, $, (,),., [, |, *,?, , / AND {. E I hope that you have a little bit of a little bit to the regular arithmetic. Now let us see two examples of a string in the information with a regular operation.

/ fm_ & y Regular Accurate Example G & E '} A example 1 (www /.)? /. (com | net | org) $ / gzcv ^ put all things together, our regular operation can be used as Check a function variable name, such as: j BC example 2` 3V Because I live in Sydney, Australia, let us check a typical Australian international phone number. The format of the Australian international phone number is as follows: / * TMT 61X xxxx-xxxx fmr / "The first X is the area code, the other is the phone number. Check the beginning of ' 61' and follow one between 2 to 9 The phone number of the area code, we use the following regular operation: x7) o ^ / 61 [2-9] [[: space:]]; z / note, the above search mode puts ' ' character " / 'Escape so that you can include it in your search, not to be interpreted as a regular operation. [2-9] Tell the regular arithduated engine We need to include a number between 2 to 9. [: Space: ]] The class tells the regular operational expectation there is a blank.% T: here is the remaining search mode: PK?) ^ [0-9] - [0-9] $ n here is not unusual Place, we just tell the regular arithmetic enginephone number available, it must be a combination of 4 digits, follow a consecutive, followed by another 4 digits, and then a string tail niece.

转载请注明原文地址:https://www.9cbs.com/read-78850.html

New Post(0)