How do I do this in Java Regex?

Trying to make a regular expression that captures all words, for example, let's just say chicken, which is not in brackets. So that,

chicken 

Will be selected but

 [chicken] 

Not. Does anyone know how to do this?

+4
source share
4 answers
 String template = "[chicken]"; String pattern = "\\G(?<!\\[)(\\w+)(?!\\])"; Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(template); while (m.find()) { System.out.println(m.group()); } 

It uses a combination of negative outlook and negative outlook and >

 (?<!\\[) //negative look behind (?!\\]) //negative look ahead (\\w+) //capture group for the word \\G //is a boundary matcher for marking the end of the previous match 

(check out the following changes for clarification)

EDIT 1:
If you need to take into account situations such as:

 "chicken [chicken] chicken [chicken]" 

We can replace the regular expression with:

 String regex = "(?<!\\[)\\b(\\w+)\\b(?!\\])"; 

EDIT 2:
If you also need to consider situations such as:

 "[chicken" "chicken]" 

As in one, "chicken" is still required, you can use:

 String pattern = "(?<!\\[)?\\b(\\w+)\\b(?!\\])|(?<!\\[)\\b(\\w+)\\b(?!\\])?"; 

In fact, these are two cases where there is only one bracket on both sides. He accomplishes this through | that acts like a or, and using ? after waiting / lag, where ? means 0 or 1 of the previous expression.

+7
source

I think you want something like:

 final Pattern UNBRACKETED_WORD_PAT = Pattern.compile("(?<!\\[)\\b\\w+\\b(?!])"); private List<String> findAllUnbracketedWords(final String s) { final List<String> ret = new ArrayList<String>(); final Matcher m = UNBRACKETED_WORD_PAT.matcher(s); while (m.find()) { ret.add(m.group()); } return Collections.unmodifiableList(ret); } 
+2
source

Use this:

 /(?<![\[\w])\w+(?![\w\]])/ 

ie, consecutive dictionary characters without a square bracket or word character before or after.

You need to check this both to the left and to the right both for the square bracket and for the word character, otherwise it will simply return to enter [chicken]

 hicke 
0
source

Do not look back:

 import java.util.regex.Pattern; import java.util.regex.Matcher; public class MatchingTest { private static String x = "pig [cow] chicken bull] [grain"; public static void main(String[] args) { Pattern p = Pattern.compile("(\\[?)(\\w+)(\\]?)"); Matcher m = p.matcher(x); while(m.find()) { String firstBracket = m.group(1); String word = m.group(2); String lastBracket = m.group(3); if ("".equals(firstBracket) && "".equals(lastBracket)) { System.out.println(word); } } } } 

Output:

 pig chicken 

A little more detailed, of course, but I find it more understandable and understandable. Of course, simpler than a huge regex trying to handle all possible combinations of parentheses.

Note that this will not filter out input, for example [fence tree grass] ; this will mean that tree is a match. You cannot miss a tree in this without a parser. Hope this is not the case you need to handle.

0
source

Source: https://habr.com/ru/post/1494585/


All Articles