5

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));
}
1
  • ==External links== and ==Further reading== have space characters in them which will not be matched by [A-Za-z]. An improvement would be to change [A-Za-z] to [A-Za-z ] but I suspect there are still additional issues with your regex. Commented Sep 18, 2013 at 18:10

1 Answer 1

4

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"
Sign up to request clarification or add additional context in comments.

4 Comments

I had used the whitespace, but i wasnt using the quantifier. Worked perfectly. Thanks.
=+ can cause mismatch. This regex will match =Lukes|editor1-first= and =Carrithers|}} * ==. Pattern.compile("==[^=]+=="); seems to be better here.
@Pshemo : I might need to match string like ===Example=== or =Example=. The number of "='s" is not fixed. i am sorry i didn't these them in the examples.
@Nikhil. Would you like to match - ==Example=?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.