DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Regexp help  (Read 10,429 times)

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Regexp help
« on: November 05, 2014, 01:00 AM »
Alright, I am trying to create a regexp that performs the following:

Selects an IP address from a line but only if the line starts with a pre-defined text string. There will be text in between the text string and the IP address -- I want to ignore the entire line except for the IP address. I know this is do-able but it is driving me nuts.

Any ideas?
Strength in Knowledge

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #1 on: November 05, 2014, 01:23 AM »
^definedstring.+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}).+$

Or perhaps:

^definedstring.+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).+$

But, that should do it or at least give you enough to test & tweak. You'll have 1 capture group with the IP address.

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #2 on: November 05, 2014, 01:40 AM »
This is better:

^definedstring.+([1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}).+$

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #3 on: November 05, 2014, 01:58 AM »
So...Here is a single line of what I am trying to filter:

SOME DATE TIME GROUP HERE :: CtrlChan Some more text here [192.192.192.192]

I want to filter out the IP address in any line that contains CtrlChan.
Strength in Knowledge

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #4 on: November 05, 2014, 02:01 AM »
Sample line

2014-10-10 19:28:01.110::ErrorMsg::CtrlChan Error_Decode [192.192.192.192]

If, and only if, the line has CtrlChan in it do I want to extract the IP address from the line. This will be a very large file (200+MB per file) with a large number of different entries.
Strength in Knowledge

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #5 on: November 05, 2014, 02:20 AM »
That helps.

This works, but you must specify that the search is multiline in order for the ^ and $ to operate properly:

^.+CtrlChan .+[^0-9]([1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}\.[1-9][0-9]{0,2}).+$

Here's the result in Expresso:

[attachimg=#]

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #6 on: November 05, 2014, 02:25 AM »
And here's the analysis to help explain what's happening in there:

[attachimg=#]

Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regexp help
« Reply #7 on: November 05, 2014, 02:33 AM »
2014-11-05_13-31-34.png

With a little help from here.

NOTE: It doesn't check if the IP is from a valid range like @Ren's but on the other hand it won't miss any if the IP is invalid.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903
« Last Edit: November 05, 2014, 03:06 AM by 4wd »

Josh

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 3,411
    • Gathering Badge
    • Official Forum Clown
    • Coding Snacks
    • Plugin Coding Medal
    • Volunteers/Staff
    • Military Service Badge - US Army
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #8 on: November 05, 2014, 02:49 AM »
Renegade and 4wd, I owe you both big time! You both just helped me save a large amount of time by not having to search these files manually. Thanks!
Strength in Knowledge

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regexp help
« Reply #9 on: November 05, 2014, 02:58 AM »
Actually, @Ren's does pick up invalid IPs ... Sorry  :-[

Honestly @Ren, you should have limited the number range it'll match on then I wouldn't have been wrong :P
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Renegade

  • Tell me something you don't know...
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 13,291
    • Participated in NANY Challenge Three Times
    • Furry Fandom Badge - Furry Fandom
    • Linux User
Re: Regexp help
« Reply #10 on: November 05, 2014, 03:25 AM »
Yeah... generally you can be pretty sloppy when checking for IP addresses though as they're unlikely to be reported badly, and few other strings will match improperly, e.g. version numbering with major, minor, revision, and build -- 10.23.34.987.

Here are some URLs for regex to actually do IP addresses properly:

http://www.shellhacks.com/en/RegEx-Find-IP-Addresses-in-a-File-Using-Grep

http://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/

http://stackoverflow.com/questions/15875013/extract-ip-addresses-from-strings-using-regex

They're kind of ugly with a lot of pipes.

@4wd - Your regex there is elegant and terse. It's one of the things that I tend to avoid as I find it's simply easier to read when being a bit more verbose. I also try to avoid a lot of special matching characters as in general as a lot of the regex I use are in EditPlus, and it's not really the best there, so it encourages being verbose rather than elegant.
Slow Down Music - Where I commit thought crimes...

Freedom is the right to be wrong, not the right to do wrong. - John Diefenbaker

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Regexp help
« Reply #11 on: November 05, 2014, 04:42 AM »
@4wd - Your regex there is elegant and terse.

The IP bit wasn't mine, it came from here.

Quote
It's one of the things that I tend to avoid as I find it's simply easier to read when being a bit more verbose.

Conversely, I find the opposite :)

I have more chance of understanding it if I'm not going cross-eyed trying to take in 50+ characters at once ;D
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Original address: https://www.donationcoder.com/forum/index.php?topic=39481.0