多行正则表达式匹配器
有输入文件,内容为:
XX00002200000
XX00003300000
regexp:
(.{6}22.{5}\W)(.{6}33.{5})
在 The Regex Coach(用于正则表达式测试的应用程序)中尝试过,字符串匹配正常。
Java:
pattern = Pattern.compile(patternString);
inputStream = resource.getInputStream();
scanner = new Scanner(inputStream, charsetName);
scanner.useDelimiter("\r\n");
patternString
是从 .xml 添加为 bean 属性的正则表达式(上面提到的),
它在 Java 中失败了。
There is input file with content:
XX00002200000
XX00003300000
regexp:
(.{6}22.{5}\W)(.{6}33.{5})
Tried in The Regex Coach(app for regexp testing), strings are matched OK.
Java:
pattern = Pattern.compile(patternString);
inputStream = resource.getInputStream();
scanner = new Scanner(inputStream, charsetName);
scanner.useDelimiter("\r\n");
patternString
is regexp(mentioned above) added as bean property from .xml
It's failed from Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的解决方案:
".{6}22.{5}\\s+.{6}33.{5}"
。请注意,\s+
是后续空格的 简写元素。下面是一个示例:
对于输出:
要使用 Java Regex,您可以使用:正则表达式编辑器(免费在线编辑器)
编辑:我认为您在读取数据时正在更改输入,请尝试:
或者
使用如下导入:
Simple solution:
".{6}22.{5}\\s+.{6}33.{5}"
. Note that\s+
is a shorthand for consequent whitespace elements.Heres an example:
With output:
To play around with Java Regex you can use: Regular Expression Editor (free online editor)
Edit: I think that you are changing the input when you are reading data, try:
Or
With imports like:
尝试在分隔符中进行此更改:
另外,为什么不使用更通用的正则表达式,如下所示:
上面提到的正则表达式适用于 2 行。既然您提到分隔符作为新行,您应该给出合适的正则表达式对于单行。
Try this change in delimiter:
also why don't you use a more general regex expression like this :
The regex you have mentioned above is for 2 lines.Since you have mentioned the delimiter as a new line you should be giving a regex expression suitable for a single line.
请原谅我的无知,但我仍然不确定你到底想搜索什么。如果您尝试搜索字符串(带有新行)
,那么为什么要通过用新行分隔它来读取它?
要按原样读取上面的字符串,请使用以下代码
注意:这里 new.txt 文件包含该字符串
Pardon my ignorance, but I am still not sure what exactly are you trying to search. In case, you are trying to search for the string (with new lines)
then why are you reading it by delimiting it by new lines?
To read the above string as it is, the following code works
NB: here new.txt file contains the string