在尝试在Java中使用JAVA中的正则表达式匹配多行文本时,使用多行文本匹配的正则表达式来使用某些考虑。模式.multiniline修饰符和(?m)速记似乎可以呈现出意外的结果。
在您的示例中,使用string.matches时(?m)模式失败,因为matches()要求regex匹配整个字符串。 Since your regex (\W)(\S) matches only a portion of the string, the comparison yields false.
To find a string that starts with "User Comments:", a regular expression that better suits the task is:
^\\s*User Comments:\\s*(.*)
This regex使用模式。dotall允许点匹配线路断裂,并捕获以下文本“用户注释:”到第一个捕获组中。通过使用dotall修改器并利用正确的匹配方法(find(find()或matches()),您可以使用Java中的Dardareions在Java中有效地匹配多行式文本。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3