0

Is there a regex, which will read and return the ip address in proper format in this text? Note it's backward, so the regex would have to step through the code backwards then replace '|' with '.'

It's been driving me crazy... and I'd appreciate your help ;)

Excerpt From Code:

4.3/d/c/b.a",10:"0://8.7/1/1.f",z:"0://8.7/1/y.x",w:"0://6.5.4.3/i/v/u.t",s:"r",q:p,o:n,9:\'0\',m:[{2:"l",k:"0://8.7/1/1.f"},{2:"j",h:{e:\'0://6.5.4.3/d/c/b.a\',\'9\':\'0\'}},{2:"g"}]});',36,40,'|type|187|20|207|31|com|hystorystingbulk|

Desired Return:

31.207.20.187

Thanks!

Edit: I have bits and pieces, but I can't put it together to get me the desired outcome

(?<=/|^) -- a positive lookbehind

\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b -- this will get you the ip in a given file/text

\b\d{1,3}\|\d{1,3}\|\d{1,3}\|\d{1,3}\b --this returns the ip (but backwards. remember I wanted it flipped) and separated by '|' instead of '.'

Someone asked....I'm using Python and testing using http://www.regexplanet.com/advanced/python/index.html THX

Fuzzy
  • 3,810
  • 2
  • 15
  • 33
user2957951
  • 303
  • 2
  • 4
  • 11
  • Could you post the code you already tried? This is not a 'we will write your code for free'-site. – Martin Tournoij Feb 16 '14 at 01:11
  • Also, it might be a good idea to specify which regex engine (or at least, which language) you're using. There are so many differences between PCRE and POXIX extended regexes, for example. – jpaugh Feb 16 '14 at 01:17
  • There's nothing *simple* about regexes, that's for sure: why not download python and use it locally? Or look for an online full-blown interpreter? Why must you use this site in particular? – jpaugh Feb 16 '14 at 01:43

2 Answers2

1

Probably the best way is to store each number individually into a group, then format and join the groups together later.

Some such:

/(\d+)\|(\d+)\|(\d+)|(\d+)/

That creates 5 different match groups, one for each set of parents ( ) and one around the whole match. But this ain't Perl! Now, in Python that would have to be more like

match("(\\d+)\\|(\\d+)\\|(\\d+)|(\\d+)", ..)

Then, use group to grab out the individual matches and put them together in the right order:

group(...) "." group(...) "." group(...) "." group(...)

Tada!

Disclaimer: This is not really Pythonic syntax, but should give you an idea.

jpaugh
  • 6,634
  • 4
  • 38
  • 90
0
import re
expression = re.compile("(\d{1,3})\|(\d{1,3})\|(\d{1,3})\|(\d{1,3})")
text = """4.3/d/c/b.a",10:"0://8.7/1/1.f",z:"0://8.7/1/y.x",w:"0://6.5.4.3/i/v/u.t",s:"r",q:p,o:n,9:\'0\',m:[{2:"l",k:"0://8.7/1/1.f"},{2:"j",h:{e:\'0://6.5.4.3/d/c/b.a\',\'9\':\'0\'}},{2:"g"}]});',36,40,'|type|187|20|207|31|com|hystorystingbulk|"""

matches = expression.findall(text)

IPs = ['.'.join(reversed(match)) for match in matches]

>>> print(IPs)
['31.207.20.187']
Adam Smith
  • 52,157
  • 12
  • 73
  • 112