仅当没有第二组(包括变体)时,正则表达式提取第一组+第二组或第一组

时间:2012-03-30 16:12:24

标签: regex

解释它的最佳方式是准确显示我想要实现的目标:

  • 案例1:"search for fenway park in boston"
    提取物:组1 - > "fenway park",第2组 - > "boston"

  • 案例2:"search for fenway park"
    提取物:组1 - > "fenway park"

请注意,在这两种情况下,我希望能够满足"search""look for""find"等等)和{{1}的变体形式("in""at"等等......)。

我尝试了很多不同的变体,但要么最后在第1组中提取"around"而在第2组中没有提取任何内容,要么在案例1正确的情况下,案例2将无效。

1 个答案:

答案 0 :(得分:5)

这应该对你有用

^(?:search for|look for|find)\s*(.*?)(?:\s*(?:in|around|at)\s*(.*))?$

您可以通过向非捕获组添加moer 子句来添加更多子句,例如look for/in/at

<强>解释

@"
^                   # Assert position at the beginning of a line (at beginning of the string or after a line break character)
(?:                 # Match the regular expression below
                       # Match either the regular expression below (attempting the next alternative only if this one fails)
      search\ for         # Match the characters “search for” literally
   |                   # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
      look\ for           # Match the characters “look for” literally
   |                   # Or match regular expression number 3 below (the entire group fails if this one fails to match)
      find                # Match the characters “find” literally
)
\s                  # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
(                   # Match the regular expression below and capture its match into backreference number 1
   .                   # Match any single character that is not a line break character
      *?                  # Between zero and unlimited times, as few times as possible, expanding as needed (lazy)
)
(?:                 # Match the regular expression below
   \s                  # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
      *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   (?:                 # Match the regular expression below
                          # Match either the regular expression below (attempting the next alternative only if this one fails)
         in                  # Match the characters “in” literally
      |                   # Or match regular expression number 2 below (attempting the next alternative only if this one fails)
         around              # Match the characters “around” literally
      |                   # Or match regular expression number 3 below (the entire group fails if this one fails to match)
         at                  # Match the characters “at” literally
   )
   \s                  # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
      *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   (                   # Match the regular expression below and capture its match into backreference number 2
      .                   # Match any single character that is not a line break character
         *                   # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
   )
)?                  # Between zero and one times, as many times as possible, giving back as needed (greedy)
$                   # Assert position at the end of a line (at the end of the string or before a line break character)
"