1

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
  • 97
  • 11

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
  • 561,645
  • 34
  • 376
  • 476
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
  • 97
  • 6