Three suggestions:
If the lines are enclosed only between brackets, then you do not need to check them at all and just use "[^"]*" as your regular expression and find all matches (provided there are no escaped quotes).
If this does not work, because the lines may occur in other places where you do not want to write them, do it in two stages.
- Corresponds to
\[[^\]]*\] . - Find all occurrences of
"[^"]*" as a result of the first match. Or even use the JSON parser to read this line.
The third possibility, a little cheating:
Search for "[^"\[\]]*"(?=[^\[\]]*\]) . This will match the line only if the next bracket following it is a closing bracket. Restriction: Do not inside the lines parentheses are allowed.I find this ugly, especially if you look at how it will look in Java:
List<String> matchList = new ArrayList<String>(); Pattern regex = Pattern.compile("\"[^\"\\[\\]]*\"(?=[^\\[\\]]*\\])"); Matcher regexMatcher = regex.matcher(subjectString); while (regexMatcher.find()) { matchList.add(regexMatcher.group()); }
Do you think anyone who looks at this after a few months might say what he is doing?
source share