如何在正则表达式中定义切换案例?

时间:2013-04-14 18:03:15

标签: java regex

如何创建执行以下操作的单个正则表达式:

“一般来说,做一个通用的正则表达式。但是如果特定的字符互相跟随,请以不同的方式检查这些特定字符后面的字符串”?

one == two && three | four

一般的正则表达式是:[&|=\s]+

分割时会导致:one, two, three, four

但是,如果每次有=个字符时我想要应用不同的正则表达式,并希望=之后的表达式仅停留在|字符,该怎么办? 这样我就得到了结果:one, two && three, four

我怎么能这样做?

1 个答案:

答案 0 :(得分:7)

这是一种可能性:

(?=[&|=\s])[&|\s]*(?:=[^|]*?[|][&|=\s]+)?

或者在自由间隔模式下有解释:

(?=[&|=\s])  # make sure there is at least a single separator character ahead;
             # this is important, otherwise you would get empty matches, and
             # depending on your implementation split every non-separator
             # character apart.
[&|\s]*      # same as before but leave out =
(?:          # start a (non-capturing) group; it will be optional and treat the
             # special =...| case
  =          # match a literal =
  [^|]*?     # match 0 or more non-| characters (as few as possible)
  [|]        # match a literal | ... this is the same as \|, but this is more
             # readable IMHO
  [&|=\s]+   # and all following separator characters
)?           # make the whole thing optional

Try it out.

修改

我刚刚意识到,这会吞噬中心部分,但你也希望将其归还。在这种情况下,您可能最好使用匹配而不是拆分(使用find)。这种模式可以解决问题:

=[&|=\s]*([^|]+?)[&|=\s]*[|]|([^&|=\s]+)

现在,第一个或第二个捕获组将包含所需的结果。这是一个解释:

#this consists of two alternatives, the first one is the special case
  =            # match a literal =
  [&|=\s]*     # consume more separator characters
  ([^|]+?)     # match 1 or more non-| characters (as few as possible) and
               # capture (group 1)
  [&|=\s]*     # consume more separator characters
  [|]          # match a literal |
|              # OR
  ([^&|=\s]+)  # match 1 or more non-separator characters (as many as possible)
               # and capture (group 2)

Try it out.