4

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

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

one == two && three | four

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

拆分时会导致:one, two, three, four.

但是,如果我想在每次有=字符时应用不同的正则表达式,并且希望后面的表达式=只在|字符处停止呢?这样我就可以得到结果:one, two && three, four.

我怎么能这样做?

4

1 回答 1

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

试试看。

编辑:

我刚刚意识到,这吞噬了中心部分,但你也想归还它。在这种情况下,您最好使用匹配而不是拆分(使用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)

试试看。

于 2013-04-14T18:11:42.623 回答