regex - Contents within an attribute for both single and multiple ending tags -


how can fetch contents within value attribute of below tag across files

<h:graphicimage .... value="*1.png*" ...../> <h:graphicimage .... value="*2.png*" ....>...</h:graphicimage> 

my regular expression search result should result into

  • 1.png
  • 2.png

all find content multiple ending tags single ending tags.

use xml parser instead, regex cannot parse xml properly, unless know input follow particular form.

however, here regex can use extract value attribute of h:graphicimage tags, read caveats after:

<h:graphicimage[^>]+value="\*(.*?)\*" 

and 1.png or 2.png in first captured group.

caveats:

  • here have assumed 1.png, 2.png etc surrounded asterisks seems question (that \* for)
  • this regex fail if 1 of attributes has ">" character in it, example

    <h:graphicimage foo=">" value="*1.png*" 

    this mentioned before regex never being able parse xml properly. work around adjusting regex:

    <h:graphicimage.+?+value="\*(.*?)\*" 

    but means if had <h:graphicimage /><foo value="*1.png*"> 1.png foo tag extracted, when want extract graphicimage tag. again, regex have issues corner cases xml, need adjust according application (for example, if know only graphicimage tag ever have "value" attribute, second case may better first).


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -