简体   繁体   中英

RegEx find text within / surrounded by string(s) | Pattern.matches() == false

I have a the following json string, which I have to filter to get the so called raw sentence.

{"subscribe":["message","sentence"," ANYTEXTWITHANYLENGTHHEREINCLUDINGNUMBERSDOTS ;\\r\\n\\"]}

Deserialization of the json string is not allowed!

I did filter the searched text with the following regular expression :

((?<=\{\"subscribe\":\[\"message\",\"sentence\",")(.*)(?=;))

or (since it has to be escaped in the java code):

String pattern = "(?<=\\{\"subscribe\":\\[\"message\",\"sentence\",\")(.*)(?=;)";

This is working perfectly if I use Matcher.find() and iterate within a while statement:

while (_regexMatcher.find()) {
    matches.add(_regexMatcher.group()); //Add to List<String>
}

Unfortunately the whole code is given by our professor. He did surround the whole code with:

if (Pattern.matches(pattern, json)) {
     ... 
}

Since Pattern.matches() uses the pattern with ^ in the beginnen of the whole pattern and $ at the ending, so it won't find any matches.
Is there another way to get the ANYTEXTWITHANYLENGTHHEREINCLUDINGNUMBERSDOTS so Pattern.matches() returns true?

You can try with:

[\s\S]*(?<=\{"subscribe":\["message","sentence",")(.*)(?=;)[\s\S]*

the [\\s\\S]* will match anything before and after, so the whole string will be matched, but only if there will be (?<=\\{"subscribe":\\["message","sentence",")(.*)(?=;) inside.

while (in_file.hasNextLine()) 
    {
        one_line = in_file.nextLine();

        tokens = one_line.split("\\P{Alpha}+");
        for (i = 0; i < tokens.length; i++) 
        {
            //do whatever;
        }

more detailed info here http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Just add .*? in front of your regular expression. It will match reluctantly (by opposition with the greedy .*), which means it will stop matching as soon as it reaches your previous json pattern, which should work as previously

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