官术网_书友最值得收藏!

The basic rules of regular expressions

Many of you are familiar with wild cards (in the Unix world, it is called glob pattern) matching of text. Here:

  • ? matches any single character
  • * matches any sequence of characters
  • [abc] matches any one character inside square brackets, so it will match a, b, or c

The regular expression pattern goes many steps farther than wild cards, where one can set many rules in a regex pattern, such as the following:

  • Match a character or a group of characters optionally (0 or 1 times)
  • Use quantifiers in regex patterns to match variable length text
  • Use a character class to match one of the listed characters or match a range of characters
  • Use a negated character class to match any character except those matched by the character class
  • Match only certain character categories, such as match only digits, only upper case letters, or only punctuation characters
  • Match a character or a group of characters for a specific length.
  • Match a length range, such as allow only six to 10 digits in the input or match an input of a minimum of eight characters
  • Use Boolean "OR" in an alternation to match one of the few alternative options
  • Use groups in regex patterns and capture substrings that we want to extract or replace from a given input
  • Alter the behavior of matching by keeping it greedy (eager), lazy (reluctant), or possessive
  • Use back references and forward references of groups that we capture
  • Use zero-width assertions such as the following:
    • Start and end anchors
    • Word boundary
    • Lookahead and lookbehind assertions
    • Start a match from the end of a previous match

For example, in a regex to match a or b we can use the following alternation:

a|b

To match one or more instances of the digit 5, we can use the following:

5+

To match any substring that starts with p and ends with w, we can use the following:

p.*w
主站蜘蛛池模板: 革吉县| 福安市| 鄯善县| 舟山市| 沁源县| 宜丰县| 娱乐| 泌阳县| 佛坪县| 宝清县| 鹤壁市| 阳东县| 宜阳县| 蒲城县| 余姚市| 丹凤县| 视频| 富锦市| 青田县| 龙江县| 尚志市| 讷河市| 蒙山县| 平武县| 庆城县| 定州市| 临沭县| 吉木萨尔县| 东莞市| 蕲春县| 宝应县| 抚顺县| 缙云县| 根河市| 杂多县| 泰和县| 左云县| 广南县| 海宁市| 南郑县| 铁岭市|