Search code examples
regexregular-language

Single result from two group Regular Expression?


I would like to take single value from the following strings(HTML tag).

<input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="6857842162548399092:-3372646398158034589" autocomplete="off" />

OR

<update id="j_id1:javax.faces.ViewState:0"><![CDATA[9028765775789786807:-4669779095536779687]]></update>

For <input> tag, the expression is

<input type="hidden" name="javax\.faces\.ViewState" id=".*\:javax.faces.ViewState\:.*" value="[^"]+".*\/>

For <update> tag, the expression is

<update .*><!\[CDATA\[(.*?.*)\]\]><\/update>

In need single result 685784..89 or 90287..87.

The expression have to get single result for <input ...> or <update....>.


Solution

  • You can combine the two with an |("OR").

    Assuming the PCRE regex flavour:

    (?|(?:<input type="hidden" name="javax\.faces\.ViewState" id=".*\:javax.faces.ViewState\:.*" value="(?<value>[^"]+)".*\/>)|(?:<update .*?><!\[CDATA\[(?<value>.*?)\]\]><\/update>))
    

    Explanation