简体   繁体   中英

How to match any symbol including a line terminator?

I have a text "text abc". When I use the "(.+) abc" pattern, I'm found a match "text", it is ok. But if I use a second pattern, "([.]{1,}) abc", matcher doesn't found any match. Why?

I want use brace [], because I want use also a break line symbol (eg, now I can't match "text \\n abc" with first pattern.

Sorry for my bad English

See also DOTALL pattern: https://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#DOTALL

My code:

    String text = "text abc";
    Pattern pattern = Pattern.compile("(.+) abc");
    Matcher m = pattern.matcher(text);
    while (m.find()) {
        System.out.println(m.group(1));
    }

PS: my goal is using somesing like "([.\\n]{1,}) abc" pattern.

Its not clear what you are trying to match, but if i understand correctly your problem is with "text \\n abc". That's because of the newline to fix this use this

Pattern pattern = Pattern.compile("(.+) abc", Pattern.DOTALL);

Pattern.DOTALL flag for "." to match even \\r or \\n.

Right now, the pattern ([.\\n]{1,}) abc is rather backwards. First off, the {1,} is identical to + , so this pattern is really ([.\\n]+) abc . Secondly, as stated in the Oracle regex tutorials:

Note: In certain situations the special characters listed above will not be treated as metacharacters.

In the case [.] , . is no longer a regex meta character. You can verify this by testing the pattern against the string "test . abc" , which will group "." .

To enable the DOTALL flag, just add the parameter to the regex or you can add the option as a parameter:

Pattern pattern = Pattern.compile("(?s)(.+) abc")

Pattern pattern = Pattern.compile("(.+) abc", Pattern.DOTALL)

Note reference

Full list of flags and other useful information

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