Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pattern Matching for java using regex

Tags:

java

regex

I have a Long string that I have to parse for different keywords. For example, I have the String:

"==References== This is a reference ==Further reading== *{{cite book|editor1-last=Lukes|editor1-first=Steven|editor2-last=Carrithers|}} * ==External links=="

And my keywords are

'==References==' '==External links==' '==Further reading=='

I have tried a lot of combination of regex but i am not able to recover all the strings.

the code i have tried:

Pattern pattern = Pattern.compile("\\=+[A-Za-z]\\=+");
Matcher matcher = pattern.matcher(textBuffer.toString());

while (matcher.find()) {
    System.out.println(matcher.group(0));
}
like image 243
Nikhil Avatar asked Oct 03 '22 22:10

Nikhil


1 Answers

You don't need to escape the = sign. And you should also include a whitespace inside your character class.

Apart from that, you also need a quantifier on your character class to match multiple occurrences. Try with this regex:

Pattern pattern = Pattern.compile("=+[A-Za-z ]+=+");

You can also increase the flexibility to accept any characters in between two =='s, by using .+? (You need reluctant quantifier with . to stop it from matching everything till the last ==) or [^=]+:

Pattern pattern = Pattern.compile("=+[^=]+=+");

If the number of ='s are same on both sides, then you need to modify your regex to use capture group, and backreference:

"(=+)[^=]+\\1"
like image 146
Rohit Jain Avatar answered Oct 13 '22 10:10

Rohit Jain