toto
toto

Reputation: 1190

Java RegExp get string between tags

I need a regexp to get all string inside a tags like it: <- -> inside this tags include any character, numbers, spaces, carriage return etc. i have this regexp:

Pattern.compile("<-(.+?)->") 

but it not detect a special sequence as: \r \n etc.

Upvotes: 1

Views: 432

Answers (3)

Ibrahim Najjar
Ibrahim Najjar

Reputation: 19423

You can try the following:

<-([\\S\\s]+?)->

Upvotes: 1

anubhava
anubhava

Reputation: 784918

but it not detect a special sequence as: \r \n etc

It won't match newline unless you use Pattern.DOTALL flag as in:

Pattern p = Pattern.compile("<-(.+?)->", Pattern.DOTALL);

OR else you can use (?s) flag:

Pattern p = Pattern.compile("(?s)<-(.+?)->");

Pattern.DOTALL makes dot match new line character so .+? will also match \r,\n etc.

Upvotes: 2

The . character does not match line breaks. This is why you are having the issue.

Upvotes: 0

Related Questions