我试图了解以下内容。

^([^=]+)(?:(?:\\=)(.+))?$


有任何想法吗?

在这里使用。显然,它是命令行解析器,但是我试图理解语法,因此我可以实际运行该程序。这来自commandline-jmxclient,他们没有有关设置JMX属性的文档,但是在其源代码中有这样的选项,所以我只想了解如何调用该方法。

  Matcher m = Client.CMD_LINE_ARGS_PATTERN.matcher(command);
  if ((m == null) || (!m.matches())) {
    throw new ParseException("Failed parse of " + command, 0);
  }

  this.cmd = m.group(1);
  if ((m.group(2) != null) && (m.group(2).length() > 0))
    this.args = m.group(2).split(",");
  else
    this.args = null;

最佳答案

很好的解释是这样的:

"
^           # Assert position at the beginning of the string
(           # Match the regular expression below and capture its match into backreference number 1
   [^=]        # Match any character that is NOT a “=”
      +           # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
(?:         # Match the regular expression below
   (?:         # Match the regular expression below
      =           # Match the character “=” literally
   )
   (           # 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 one 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 the string (or before the line break at the end of the string, if any)
"


它将捕获=之前的所有内容以进行反向引用1以及之后的所有内容进行反向引用2。

例如

33333098320498
adhajdh =3232-40923-04924-0924


对于第一个字符串,所有内容都捕获到$ 1中。

对于第二个:

adhajdh  <---------- captured to $1
3232-40923-04924-0924 <----- captured to $2

关于java - 了解此正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8191199/

10-14 10:51