I have several strings which look like the following:
<some_text> TAG[<some_text>@11.22.33.44] <some_text>
I want to get the ip_address and only the ip_address from this line. (For the sake of this example, assume that the ip address will always be in this format xx.xx.xx.xx)
Edit: I'm afraid I wasn't clear.
The strings will look something like this:
<some_text> TAG1[<some_text>@xx.xx.xx.xx] <some_text> TAG2[<some_text>@yy.yy.yy.yy] <some_text>
Note that the 'some_text' can be a variable length. I need to associate different regex's to different tags so that when r.group() is called, the ip address will be returned. In the above case the regex's would not be different but it is a bad example.
The regexes I have tried so far have been inadequate.
Ideally, I would like something like this:
r = re.search('(?<=TAG.*@)(\d\d.\d\d.\d\d.\d\d)', line)
where line is in the format specified above. However, this does not work because you need to have a fixed width look-behind assertion.
Additionally, I have tried non-capturing groups as such:
r = re.search('(?<=TAG\[)(?:.*@)(\d\d.\d\d.\d\d.\d\d)', line)
However, I cannot use this because r.group() will return [email protected]
I understand that r.group(1) will return just the ip address. Unfortunately, the script I am writing requires that all my regex will return the correct result after calling r.group().
What kind of regex could I use for this situation?
The code is in python.
Note: All of the some_text can be variable length