简体   繁体   中英

Java regex on a long string(String buffer)

I have 2 questions actually. 1st theoretical- If I have a long text in a StringBuffer and I want to find a regular expression inside of it- do I have to worry that the StringBuffer will take chunks of the StringBuffer look for the regex inside of each chunk, and if it doesn't find such regex inside of any chunk it will appear as if the regular expression inside of the text doesn't exist? What if part of the regex is at the border of one chunk, and rest of the regex at the border of the neighbouring chunk?

2nd:

String patternString1 = "(\\[\\[Category.*\\]\\])";
Pattern pattern1 = Pattern.compile(patternString1);
Matcher matcher1 = pattern1.matcher(text);
while (matcher1.find()){
System.out.println(matcher1.group(1));
}

I want to extract from the string all expression that looks like this [[Category: .*]] but if the whole text, for example, looks like this:

[[Category: PrintingOut [[Printer HP]] [[HewlettPackard]] ]] [[LaserJet]]

I want to assure that the " ]] " is the end of my regular expression. In other words- if I find [[ .* ]] inside of my expression I don't want the ending of the inner [[.*]] to be treated as the ending of my whole expression.

This might help you.

\[\[Category:.*?\s\]\]

Here is the demo on Debuggex


EDIT

Try below regex that will work if there is space or not before closing ]]

\[\[Category:.*?[(\]\])\s]+\]\]

Here is the demo on Debuggex

Note: Just enclose this regex pattern inside () and get the group at index 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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