简体   繁体   English

正则表达式,如何编写查询

[英]Regex, how to write query

How to write a regular expression with the following text, which draws the text and number: 如何用以下文本编写正则表达式,以绘制文本和数字:

String txt = "Map. 12. txt Map. 13. txt Map. 14 txt "; 

I tried to write the regex in the following way: 我试图通过以下方式编写正则表达式:

 String regex = "(Map\\p{Punct}\\p{Space}])([0-9]+\\p{Punct})(.+)?(Map)";

After the distribution receives: (Map. 12.) (Txt) (Map), which causes the next partition starts from Map. 分发收到后:(Map。12.)(Txt)(Map),这导致下一个分区从Map开始。 14. skipping the text contained between Map 13 and Map 14. 14.跳过地图13和地图14之间包含的文本。

Thank for your help. 谢谢您帮忙。

Seems like you want something like this, 好像您想要这样的东西,

String s = "String txt = \"Map. 12. txt Map. 13. txt Map. 14 txt ";
Matcher m = Pattern.compile("(Map\\p{Punct}\\p{Space})([0-9]+\\p{Punct})(.+?)\\s*(?=Map|$)").matcher(s);
while(m.find())
{
System.out.println(m.group());
}

Output: 输出:

Map. 12. txt 
Map. 13. txt 

This won't match the Map. 14 txt 这与Map. 14 txt不符Map. 14 txt Map. 14 txt because there isn't a punctuation exists next to the number 14. If you want to match this also, you need to make the punctuation pattern as optional like, Map. 14 txt因为数字14旁边不存在标点符号。如果您也想与此匹配,则需要将标点符号样式设为可选,例如,

"(Map\\p{Punct}\\p{Space})([0-9]+\\p{Punct}?)(.+?)\\s*(?=Map|$)" 

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

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