0

am new to regex, so am having a hard time compiling my own pattern.

Basically am reading the LogCat from my android phone and am trying to break this string into pieces to be able to display it in TextView

This is the actually string am trying to token. "07-06 12:18:45.790: W/dalvikvm(571): TaintLog: OSNetworkSystem.write(91.121.171.39) received data with tag 0x400 data=[POST / HTTP/1.1"

Am able to retrieve the number after "Tag" using the scanner method, but now I need to extract the IP address from the OSnetworkSystem.Write(xxx.xxx.xxx.xxx)

Can anybody provided me some tips on what to use to be able to extra the IP address.

1 Answer 1

1

Well, for the regex part, I can help on.

The regular expression:

([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3})

Would match any IP Address in a given string.

For RegExp I use a website, http://rubular.com/ which has really helped with my workflow. I can throw in any number of "test cases" and start writing the regex code.


Editing per comments below.

Instead of re-inventing the wheel. I found a great write up on IPv4 Regex: How do I write a regular expression that matches an IPv4 dotted address?

Which basically says:

/^0*([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.0*([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$/

It is long and complex. But following the write up, should give you some good understanding in what it actually does.

In my experience, the more complex & relient we get with regex success rates, the more it is going to probably fail. I'd suggest never relying on regex for critical data ... Find another way, there always is :)

Sign up to request clarification or add additional context in comments.

4 Comments

Here's a link to a regular expressions cheatsheet for additional help constructing other regexs: cheatography.com/davechild/cheat-sheets/regular-expressions
Are IP Addresses allowed to have values over 255? If not, you'll need a more complex regex to handle it.
Agreed with BlackVegetable. You could get much more indepth. I should add a little reminder. Never, ever, rely on RegEx to be fool-proof. There is almost always a case where RegEx will gather the wrong thing. @ardentsonata:Excellent cheat sheet! Favorite'd that one.
@BlackVegetable, another concern is whether or not he wants to be IPv6 compliant or just IPv4. Could you construct a regular expression that searches for perhaps ([:xdigit:]{0,2}\.[:xdigit:]{0,2}\.[:xdigit:]{0,2}\.[:xdigit:]{0,2}) in regards to IPv4 though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.