寻找名称年龄性别的正则表达式,中间有强制性空格

时间:2013-04-30 14:28:41

标签: regex

这就是我所拥有的,但它不起作用。我没有得到错误,但我也没有得到结果,通过或失败。

^[a-zA-Z]+(([\'\ \][(^10$|^[0-9]{1,2}]))+(([\'\ \][(?:m|M|f|F|)$]))*$

1 个答案:

答案 0 :(得分:0)

首先,让我们看看你提到的模式将与之匹配:

=====================================================================
^[a-zA-Z]+(([\'\ \][(^10$|^[0-9]{1,2}]))+(([\'\ \][(?:m|M|f|F|)$]))*$
=====================================================================

Assert position at the beginning of the string «^»
Match a single character present in the list below «[a-zA-Z]+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   A character in the range between “a” and “z” «a-z»
   A character in the range between “A” and “Z” «A-Z»
Match the regular expression below and capture its match into backreference number 1 «(([\'\ \][(^10$|^[0-9]{1,2}]))+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «+»
   Match the regular expression below and capture its match into backreference number 2 «([\'\ \][(^10$|^[0-9]{1,2}])»
      Match a single character present in the list below «[\'\ \][(^10$|^[0-9]{1,2}»
         Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
         A ' character «\'»
         A   character «\ »
         A ] character «\]»
         One of the characters “[(^10$|” «[(^10$|^[»
         A character in the range between “0” and “9” «0-9»
      Match the character “]” literally «]»
Match the regular expression below and capture its match into backreference number 3 «(([\'\ \][(?:m|M|f|F|)$]))*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «*»
   Match the regular expression below and capture its match into backreference number 4 «([\'\ \][(?:m|M|f|F|)$])»
      Match a single character present in the list below «[\'\ \][(?:m|M|f|F|)$]»
         A ' character «\'»
         A   character «\ »
         A ] character «\]»
         One of the characters “[(?:m|MfF)$” «[(?:m|M|f|F|)$»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

很明显,这种模式需要匹配上下文:

aaaa' ]'m

当你要匹配像Bob 33 M这样的东西时,那么最简单的模式就是:

  (?i)^[a-z]+ [0-9]+ [mf]+$

表示

"(?i)" &     ' Match the remainder of the regex with the options: case insensitive (i)
"^" &        ' Assert position at the beginning of the string
"[a-z]" &    ' Match a single character in the range between “a” and “z”
   "+" &        ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"\ " &       ' Match the character “ ” literally
"[0-9]" &    ' Match a single character in the range between “0” and “9”
   "+" &        ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"\ " &       ' Match the character “ ” literally
"[mf]" &     ' Match a single character present in the list “mf”
   "+" &        ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"$"          ' Assert position at the end of the string (or before the line break at the end of the string, if any)

希望 link 可以帮助您理解这一主题。