java - regex to modify dynamic src attributes -


I am trying to use regex in a particular scenario as follows: < P> Many HTML pages, with the dynamic value & lt; Img src & gt; Each containing the number of tags:

  tag1 = & lt; P & gt; Paragraph 1 & lt; Img src = "/ A / images /b.txt"> Some text & lt; / P & gt; Tag2 = & lt; P & gt; Paragraph 2 & lt; Img src = "/ a / b / images / cjpeg" & gt; Some text & lt; / P & gt; Tag 3 = & lt; P & gt; Paragraph 3 & lt; Img src = "/../ Images / H / E." & Lt; / p & gt; Tag 4 = & lt; p & gt; Paragraph 4 & lt; img src = "/../d / images / g / j / f.gif" & quot; & quot; Gt; Some text & lt; / p & gt;   

We will filter the pattern "/ < Nothing & gt; / Images / target. What we need after replacement

  Tag1 = & lt; P & gt; Paragraph 1 & lt; Img src = "/ library / MYFOLDER / location / b.txt" & gt; Some text & lt; / P & gt; Tag2 = & lt; P & gt; Paragraph 2 & lt; Img src = "/ library / MYFOLDER / location / c.jpeg" & gt; Some text & lt; / P & gt; Tag 3 = & lt; P & gt; Paragraph 3 & lt; Img src = "/ library / MYFOLDER / location / h / e page" & gt; Some text & lt; / P & gt; Tag 4 = & lt; P & gt; Paragraph 4 & lt; Img src = "/ library / MYFOLDER / location / g / j / f.gif" & gt; Some text & lt; / P & gt;   

What really is happening is very different. After the pattern is / images eat everything and give us

  tag 1 = & lt; P & gt; Paragraph 1 & lt; img src = "/ library / MYFOLDER / locationp> tag2 = & lt; p & gt; paragraph2 & lt; img src =" / library / MYFOLDER / locationp & gt; Tag 3 = & lt; P & gt; Paragraph 3 & lt; img src = "/ library / MYFOLDER / locationp>   

Here's the regex pattern I'm using

 "    {1} {1,} [/ images /] {1} of & lt (.); Br>   

Here is the code:

  string substringament = "<

Control step:

Expected Result:

  & lt; P & gt; & Lt; Strong & gt; Control step: & lt; / strong> & Lt; Img src = "/ library / MYFOLDER / location / maintenance_game.gif \" width = \ "20 \" & gt; . Details & lt; / P & gt;   

Actual results:

  & lt; P & gt; & Lt; Strong & gt; Control step: & lt; / strong> ; I is using your regex square bracket. In a regex,  [abc]  matches a letter that is either  a ,  b , or  c ; This substrings do not match  "abc" . Therefore,  [/ images /]  does not do that you think it does. Remove square brackets.  

What really happens to your regex:

  "{1} (.) {1,} [/ images /] {1}   

This will match a quote character, followed by 1 or more events of any character followed by a / , i , M , a , g , e , s . (Last / will be ignored after one of the sets you already have.) Also, when you ask to match one or more events of any character, the default This greedy match, matching as many characters as possible, therefore, it will be closed on the forefront of the square brackets instead of the nearest one place; and the top character < Try this regex instead of code: / in and

  "++ / images /   

You do not need to specify a regex to match the same event with {1} , it does for you automatically . There is a shortcode for code> + {1,} . ? Instead of the largest number, then it will search for the nearest / images / sub-code.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -