简体   繁体   English

如何在Java字符串中查找具有特定模式的子字符串

[英]How to find substrings with a certain pattern within a string in Java

I'm using PDFBox to extract the contents of a PDF file. 我正在使用PDFBox提取PDF文件的内容。 Following which I'll end up with one massive string. 接下来,我将得到一个巨大的字符串。

Now, I need to extract all substrings in this string which match the pattern XX-XXXX (where the X can be a number or an alphabet) or XX YYYY (where X is a alphabet and Y is a number). 现在,我需要提取此字符串中与模式XX-XXXX (其中X可以是数字或字母)或XX YYYY (其中X是字母而Y是数字)匹配的所有子字符串。

I tried ("([A-Z0-9]{2})-([A-Z0-9]{4})") to match the XX - XXXX pattern but it doesn't return me the expected values. 我尝试了(“([[A-Z0-9] {2})-([A-Z0-9] {4})”)来匹配XX-XXXX模式,但没有返回期望值。

Please do a short study about Regular Expressions in Java. 请对Java中的正则表达式进行简短的研究。 Once done have a look at the java.util.regex.Matcher. 完成后,看看java.util.regex.Matcher。 This helps a lot in finding text on the basis your regex. 这对基于正则表达式查找文本很有帮助。 Below is a link that will help you a lot. 以下是对您有很大帮助的链接。 http://tutorials.jenkov.com/java-regex/matcher.html http://tutorials.jenkov.com/java-regex/matcher.html

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

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