Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在正则表达式中定义开关情况?_Java_Regex - Fatal编程技术网

Java 如何在正则表达式中定义开关情况?

Java 如何在正则表达式中定义开关情况?,java,regex,Java,Regex,如何生成一个执行以下操作的正则表达式: “一般来说,使用通用正则表达式。但是如果特定字符紧跟在后面,请以不同的方式检查这些特定字符后面的字符串” 一般正则表达式是:[&|=\s]+ 拆分时会产生以下结果:1、2、3、4 但是,如果每次出现=字符时都要应用不同的正则表达式,并且希望=后面的表达式仅在字符处停止,该怎么办? 这样我就得到了结果:1,2&&3,4 我该怎么做呢?这里有一种可能性: (?=[&|=\s])[&|\s]*(?:=[^|]*?[|][&|=\s]+)?

如何生成一个执行以下操作的正则表达式:

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

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

拆分时会产生以下结果:
1、2、3、4

但是,如果每次出现
=
字符时都要应用不同的正则表达式,并且希望
=
后面的表达式仅在
字符处停止,该怎么办? 这样我就得到了结果:
1,2&&3,4


我该怎么做呢?

这里有一种可能性:

(?=[&|=\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
#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)

编辑:

我刚刚意识到,这会吞下中间部分,但你也要把它还给我。在这种情况下,您最好使用匹配而不是拆分(使用
find
)。这种模式应该可以做到:

=[&|=\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
#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)

你的问题不是很清楚(至少对我来说是这样)。你是这样想的:
[=one |][124;[=two |=three |][124;[=four |]