2

My regex:

 (<property\sname=\"uri\"\s[value=\"htttp:\/\/]+(\d+\.)+\d+)+

Text sample:

 <bean id="journeyWSClient" parent="abstractClient" class=" lib.JourneyWSClient">
        <property name="uri" value="http://192.24.342.432:20010/some/path/1_0_0"/>
        <!-- Fortuna -->        
        <!-- property name="uri" value="http://164.7.19.11:20010/some/path/1_0_0"/ -->

It works on http://regexr.com/, however when I put the regex in bash script, it doesn't work. Are there some characters I need to escape? Ideas?

Bonus cookie for extracting the IP with only one regex.

matijasx
  • 107
  • 12

2 Answers2

2

Replace the \d with [0-9] and \s with [[:space:]], and adjust the IP matching part as ([0-9]+(\.[0-9]+)+) (or simplify it to ([0-9.]+)) so as to be able to get its value with ${BASH_REMATCH[1]}:

text='<property name="uri" value="http://192.49.200.142:20010/some/path/1_0_0"/>'
regex='<property[[:space:]]name="uri"[[:space:]]value="http://([0-9]+(\.[0-9]+)+)'
if [[ $text =~ $regex ]]; then
    echo ${BASH_REMATCH[1]};
fi

See the IDEONE demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • why replacing `\d` with `[0-9]`? – JobaDiniz Apr 26 '23 at 11:45
  • @JobaDiniz POSIX regex flavor does not support `\d`. – Wiktor Stribiżew Apr 26 '23 at 11:46
  • I'm trying to convert this _semver_ regex to shell script: `^((0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*))(?:-((?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$` and it works on regex101 but not on shell :( – JobaDiniz Apr 26 '23 at 11:59
  • 1
    @JobaDiniz Sure, POSIX regex flavor does not support non-capturing groups, replace all `(?:` with just `(`. – Wiktor Stribiżew Apr 26 '23 at 12:36
0

See if this works for you..

value="http:\/\/(([0-9].?)+):

This worked for me if you have IP only with http pattern

grep http TEXT_SAMPLE_FILE |sed -E    's;.*value="http://(([0-9].?)+):.*;\1;g' 
Sanjeev
  • 107
  • 6