ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Examples of regular expressions

2022-08-27 07:32:28  阅读:227  来源: 互联网

标签:regex matches character regular Examples characters expressions line Match


https://support.google.com/a/answer/1371417?hl=en#Match-Word-or-Phrase-in-a-List

 

Examples of regular expressions

The following examples illustrate the use and construction of simple regular expressions. Each example includes the type of text to match, one or more regular expressions that match that text, and notes that explain the use of the special characters and formatting.

For additional instructions and guidelines, see also Guidelines for using regular expressions and RE2 Syntax. See also Set up rules for content compliance.

Important: We support RE2 Syntax only, which differs slightly from PCRE. Regular expressions are case-sensitive by default.

Note: Examples shown below can be useful as starting points for more complex regular expressions. However, for matching a single word, we suggest that you use the Content compliance or Objectionable content settings.

Match Exact Phrase Only
Usage example Match the phrase stock tips.
Regex examples Example 1: (\W|^)stock\stips(\W|$)

Example 2:(\W|^)stock\s{0,3}tips(\W|$)

Example 3: (\W|^)stock\s{0,3}tip(s){0,1}(\W|$)
Notes
  • \W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the phrase.
  • In example 2, \s matches a space character, and {0,3} indicates that from 0 to 3 spaces can occur between the words stock and tip.
  • ^ matches the start of a new line. Allows the regex to match the phrase if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the phrase if it appears at the end of a line, with no characters after it.
  • In example 3, (s) matches the letter s, and {0,1} indicates that the letter can occur 0 or 1 times after the word tip. Therefore, the regex matches stock tip and stock tips. Alternatively, you can use the character ? instead of {0,1}

Match Word or Phrase in a List
Usage example Match any word or phrase in the following list:
  • baloney
  • darn
  • drat
  • fooey
  • gosh darnit
  • heck
Regex example (?i)(\W|^)(baloney|darn|drat|fooey|gosh\sdarnit|heck)(\W|$)
Notes
  • (...) groups all the words, such that the \W character class applies to all of the words within the parenthesis.

  • (?i) makes the content matching case insensitive.

  • \W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the words or phrases in the list.

  • ^ matches the start of a new line. Allows the regex to match the word if it appears at the beginning of a line, with no characters before it.

  • $ matches the end of a line. Allows the regex to match the word if it appears at the end of a line, with no characters after it

  • | indicates an “or,” so the regex matches any one of the words in the list.

  • \s matches a space character. Use this character to separate words in a phrase.

Match Word with Different Spellings or Special Characters
Usage example Match the word viagra and some of the obfuscations that spammers use, such as:
  • vi@gra
  • v1agra
  • v1@gra
  • v!@gr@
Regex example v[i!1][a@]gr[a@]
Notes
  • \W isn't included, so that other characters can appear before or after any of the variants of viagra. For example, the regex still matches viagra in the following text:

viagra!! or ***viagra***

  • [i!1] matches the characters i, !, or 1 in the second character position of the word.

Match Any Email Address from a Specific Domain
Usage example Match any email address from the domains yahoo.com, hotmail.com, and gmail.com.
Regex example (\W|^)[\w.\-]{0,25}@(yahoo|hotmail|gmail)\.com(\W|$)
Notes
  • \W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the email address.
  • ^ matches the start of a new line. Allows the regex to match the address if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the address if it appears at the end of a line, with no characters after it.
  • [\w.\-] matches any word character (a-z, A-Z, 0-9, or an underscore), a period, or a hyphen. These are the most commonly used valid characters in the first part of an email address. The \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
  • The \ before the dash and period “escapes” these characters—that is, it indicates that the dash and period aren't a regex special characters themselves. There's no need to escape the period within the square brackets.
  • {0,25} indicates that from 0 to 25 characters in the preceding character set can occur before the @ symbol. The Content Compliance email setting supports matching of up to 25 characters for each character set in a regular expression.
  • The (...) formatting groups the domains, and the | character that separates them indicates an “or.”

Match Any IP Address in a Range
Usage example Match any IP address within the range 192.168.1.0 to 192.168.1.255.
Regex examples Example 1:   192\.168\.1\.
Example 2:   192\.168\.1\.\d{1,3}
Notes
  • The \ before each period “escapes” the period—that is, it indicates that the period isn't a regex special character itself.
  • In Example 1, no characters follow the last period, so the regex matches any IP address beginning with 192.168.1., regardless of the number that follows.
  • In Example 2, \d matches any digit from 0 to 9 after the last period, and {1,3} indicates that the digits 1 to 3 can appear after that last period. In this case, the regex matches any complete IP address beginning with 192.168.1.. This regex also matches invalid IP addresses, such as 192.168.1.999.

Match an Alphanumeric Format
Usage example Match the purchase order numbers for your company. This number has various possible formats, such as:
  • PO nn-nnnnn
  • PO-nn-nnnn
  • PO# nn nnnn
  • PO#nn-nnnn
  • PO nnnnnn
Regex example (\W|^)po[#\-]{0,1}\s{0,1}\d{2}[\s-]{0,1}\d{4}(\W|$)
Notes
  • \W matches any character that’s not a letter, digit, or underscore. It prevents the regex from matching characters before or after the number.
  • ^ matches the start of a new line. Allows the regex to match the number if it appears at the beginning of a line, with no characters before it.
  • $ matches the end of a line. Allows the regex to match the number if it appears at the end of a line, with no characters after it.
  • [#\-] matches a pound sign or a hyphen after the letters po, and {0,1} indicates that one of those characters can occur zero or one times. The \- (which indicates a hyphen) must occur last in the list of characters within the square brackets.
  • \s matches a space, and {0,1} indicates that a space can occur zero or one times.
  • \d matches any digit from 0 to 9, and {2} indicates that exactly 2 digits must appear in this position in the number.

 

标签:regex,matches,character,regular,Examples,characters,expressions,line,Match
来源: https://www.cnblogs.com/kungfupanda/p/16629747.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有