如果没有括号,我会如何检测一个单词的上标,但是如果有括号,它们的所有内容是什么?

时间:2014-12-15 22:36:16

标签: regex

我想检测以下两种情况,最好是一个正则表达式:

  

这是我今天写的 ^ 这句话。

  

今天是 ^(我写的)这句话。

所以基本上,如果插入符号后面有括号,我想匹配它们内部的任何内容。否则,我只想匹配下一个单词。

我是正则表达式的新手。这可能不会太复杂吗?

1 个答案:

答案 0 :(得分:1)

\^(\w+|\([\w ]+\))

Options: case insensitive; ^ and $ match at line breaks

Match the character “^” literally «\^»
Match the regular expression below and capture its match into backreference number 1 «(\w+|\([\w ]+\))»
   Match either the regular expression below (attempting the next alternative only if this one fails) «\w+»
      Match a single character that is a “word character” (letters, digits, etc.) «\w+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Or match regular expression number 2 below (the entire group fails if this one fails to match) «\([\w ]+\)»
      Match the character “(” literally «\(»
      Match a single character present in the list below «[\w ]+»
         Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
         A word character (letters, digits, etc.) «\w»
         The character “ ” « »
      Match the character “)” literally «\)»


Created with RegexBuddy