-5

This is what I have but it's not working. I'm not getting errors but I'm also not getting a result, pass or fail.

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

1 回答 1

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)

希望此链接可以帮助您理解该主题。

于 2013-04-30T15:20:54.270 回答