简体   繁体   English

当这样的字符串由多行完成时,无法将字符串与正则表达式模式匹配

[英]Cannot match string with regex pattern when such string is done of multiple lines

I have a string like the following:我有一个如下字符串:

SYBASE_OCS=OCS-12_5
SYBASE=/opt/sybase/oc12.5.1-EBF12850
//there is a newline here as well

The string at the debugger appears like this:调试器中的字符串如下所示:

在此处输入图像描述

I am trying to match the part coming after SYBASE= , meaning I'm trying to match /opt/sybase/oc12.5.1-EBF12850 .我正在尝试匹配SYBASE=之后的部分,这意味着我正在尝试匹配/opt/sybase/oc12.5.1-EBF12850

To do that, I've written the following code:为此,我编写了以下代码:

String key = "SYBASE";
Pattern extractorPattern = Pattern.compile("^" + key + "=(.+)$");
Matcher matcher = extractorPattern.matcher(variableDefinition);
if (matcher.find()) {
    return matcher.group(1);
}

The problem I'm having is that this string on 2 lines is not matched by my regex, even if the same regex seems to work fine on regex 101 .我遇到的问题是 2 行上的这个字符串与我的正则表达式不匹配,即使相同的正则表达式似乎在regex 101上工作正常。

State of my tests: State 我的测试:

  • If I don't have multiple lines (eg if I only had SYBASE=... followed by the new line), it would match如果我没有多行(例如,如果我只有SYBASE=...后跟新行),它将匹配
  • If I evaluate the expression extractorPattern.matcher("SYBASE_OCS=OCS-12_5\\nSYBASE=/opt/sybase/oc12.5.1-EBF12850\\n") (note the double backslash in front of the new line), it would match.如果我评估表达式extractorPattern.matcher("SYBASE_OCS=OCS-12_5\\nSYBASE=/opt/sybase/oc12.5.1-EBF12850\\n") (注意新行前面的双反斜杠),它将匹配.
  • I have tried to use variableDefinition.replace("\n", "\\n") to what I give to the matcher() , but it doesn't match.我尝试将variableDefinition.replace("\n", "\\n")用于我给matcher()的内容,但它不匹配。

It seems something simple but I can't get out of it.这似乎很简单,但我无法摆脱它。 Can anyone please help?有人可以帮忙吗?

Note: the string in that format is returned by a shell command, I can't really change the way it gets returned.注意:该格式的字符串由 shell 命令返回,我无法真正改变它的返回方式。

The anchors ^ and $ anchors the match to the start and end of the input.^$将匹配锚定到输入的开始和结束。

In your case you would like to match the start and end of a line within the input string.在您的情况下,您希望匹配输入字符串中一行的开头和结尾。 To do this you'll need to change the behavior of these anchors.为此,您需要更改这些锚点的行为。 This can be done by using the multi line flag.这可以通过使用多行标志来完成。

Either by specifying it as an argument to Pattern.compile :通过将其指定为Pattern.compile的参数:

Pattern.compile("regex", Pattern.MULTILINE)

Or by using the embedded flag expression: (?m) :或者通过使用嵌入的标志表达式: (?m)

Pattern.compile("(?m)^" + key + "=(.+)$");

The reason it seemed to work in regex101.com is that they add both the global and multi line flag by default:它似乎在 regex101.com 中起作用的原因是它们默认添加了全局和多行标志:

regex101 的默认标志

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM