Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring the line break in regex?

Tags:

java

regex

I have below content in text file

  some texting content <img  src="cid:part123" alt=""> <b> Test</b>

I read it from file and store it in String i.e inputString

   expectedString = inputString.replaceAll("\\<img.*?cid:part123.*?>",
    "NewContent");

I get expected output i.e

     some texting content NewContent <b> Test</b>

Basically if there is end of line character in between img and src like below, it does not work for example below

 <img  
          src="cid:part123" alt="">

Is there a way regex ignore end of line character in between while matching?

like image 537
M Sach Avatar asked Nov 06 '12 10:11

M Sach


People also ask

How do you escape a new line in regex?

In pattern matching, the symbols “^” and “$” match the beginning and end of the full file, not the beginning and end of a line. If you want to indicate a line break when you construct your RegEx, use the sequence “\r\n”.

What is \r and \n in regex?

Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

What is the use of \b in regex?

The \b metacharacter matches at the beginning or end of a word.


1 Answers

If you want your dot (.) to match newline also, you can use Pattern.DOTALL flag. Alternativey, in case of String.replaceAll(), you can add a (?s) at the start of the pattern, which is equivalent to this flag.

From the Pattern.DOTALL - JavaDoc : -

Dotall mode can also be enabled via the embedded flag expression (?s). (The s is a mnemonic for "single-line" mode, which is what this is called in Perl.)

So, you can modify your pattern like this: -

expectedStr = inputString.replaceAll("(?s)<img.*?cid:part123.*?>", "Content");

NOTE: - You don't need to escape your angular bracket(<).

like image 99
Rohit Jain Avatar answered Nov 14 '22 06:11

Rohit Jain