Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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_Jmx - Fatal编程技术网

Java 理解这个正则表达式

Java 理解这个正则表达式,java,regex,jmx,Java,Regex,Jmx,我试图理解下面的内容 ^([^=]+)(?:(?:\\=)(.+))?$ 有什么想法吗 这里正在使用这个。很明显,这是命令行解析器,但我正在尝试理解语法,以便实际运行程序。这是从,他们没有关于设置JMX属性的文档,但在他们的源代码中,有这样一个选项,所以我只想了解如何调用该方法 Matcher m = Client.CMD_LINE_ARGS_PATTERN.matcher(command); if ((m == null) || (!m.matches())) { throw

我试图理解下面的内容

^([^=]+)(?:(?:\\=)(.+))?$
有什么想法吗

这里正在使用这个。很明显,这是命令行解析器,但我正在尝试理解语法,以便实际运行程序。这是从,他们没有关于设置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之后的所有内容

e、 g

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

关于第二个问题:

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

首先,让我们确定我们谈论的是同一个正则表达式。它可能是这样创造的:

public static final Pattern CMD_LINE_ARGS_PATTERN =
    Pattern.compile("^([^=]+)(?:(?:\\=)(.+))?$");
Java编译器将中的双反斜杠(
\\=
)转换为单个反斜杠,因此
Pattern.compile()
将其视为
\=
,一个转义等号。顺便说一句,这是不需要逃避的<代码>^([^=]+)(?:=(.+)?$也同样有效

总之,该代码正在寻找以下形式之一的命令:

命令
命令=arg
command=foo,bar
命令=abc,123,xyz
……等等。第一部分regex-
([^=]+)
-捕获“命令”,即第一部分之前的所有内容都等于(如果有),或者整个字符串(如果没有)。第二部分是可选的,它周围有一个非捕获组,由一个
量词控制。如果有等号
(?:\\=)
将使用它,然后
(.+)
将捕获字符串的其余部分

如果匹配成功,命令将被捕获到组#1中,但我们还不知道是否有参数列表。如果没有等号,则第二个捕获组将不会参加比赛,
m.group(2)
将返回
null
。否则,我们将其拆分为逗号以分隔各个参数

但这段代码只带你走了这么远。它也会接受这些输入,但您必须测试它们是否有效:

command=foo,bar#周围的空间可以/将被修剪?
command=foo-bar#内部空间可以吗?
command=foo,#一项参数列表,没有问题
命令=,#零项参数列表,可能有问题

+1作为答案,我在非常罕见的情况下遇到了regex。我真的不认为那些带注释的正则表达式树是值得的;太多的细节,太多的重复,没有足够的大画面。两个最重要的答案都是正确的(Dmitry和FailedDev)。应该注意的是,这个正则表达式可以简化
public static final Pattern CMD_LINE_ARGS_PATTERN =
    Pattern.compile("^([^=]+)(?:(?:\\=)(.+))?$");