0

Here is my regex: (^[A-Za-z][A-Za-z0-9_]*)=([A-Za-z0-9, _.]+|^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))

I was hoping i could use something like this to match text in a config file of the forms:

variable=value
ipaddress=127.0.0.1

etc...

with the test string

test=127.0.0.1
test1=192.168.0.1
test2=hello world
test3=helloworld
test4=Carl_Sagan,Awesome,cool story bro
test5=%should not match(#$&!&$(5vzxcv
6test=should not match

I was testing on http://myregextester.com/index.php#sourcetab and some others

and this expression would only match the first line. When I cut out different parts of the expression, it worked how I wanted, but not when I put them together.

On the testing site http://gskinner.com/RegExr/ it works just fine.

^(?:(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])) will return all the ip address in the test string

(^[A-Za-z][A-Za-z0-9_]*)= will return all the variable= on the page

[A-Za-z0-9, _.]+ will return almost anything

Can you guys help me understand what it is I am doing wrong?

8
  • 1
    The major thing you are doing wrong is not explaining what you want. Given your input above, what is your desired output?
    – Bohemian
    Jul 29, 2011 at 14:34
  • Which language do you use? Your regex matches each of the lines separately. Do you instead want to match anything with only one regex?
    – Howard
    Jul 29, 2011 at 14:35
  • @Bohemian The desired output is every line except the ones that say should not match. which are the last two. It only gives me the first one.
    – Noob
    Jul 29, 2011 at 14:36
  • Using pretty much the same regex, just condensed I was able to match the lines you spoke of... Jul 29, 2011 at 14:37
  • 1
    If it only matched the first line, you probably forgot to specify the Multiline option.
    – Alan Moore
    Jul 29, 2011 at 17:00

2 Answers 2

0

I do not recommend using regex for ip adresses. Othervise you will have to deal with beasts like

"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
2
  • I'm not going for a very complex match on the ip address. I'm just going to let them specify a min and max to try and it will iterate up. It doesn't have to be perfect.
    – Noob
    Jul 29, 2011 at 14:43
  • plus I can just copy pasta your statement into my regex and bam, I don't even have to deal with that problem.
    – Noob
    Jul 29, 2011 at 14:45
0

This should work:

(^[A-Za-z][A-Za-z0-9_]*)=(([0-9]{1,3}\.){3,5}[0-9]{1,3}|([A-Za-z0-9, _.]*))

Check it out here: http://regexr.com?2ubgl

2
  • did you just switch the last two groups?
    – Noob
    Jul 29, 2011 at 14:48
  • I also added an extra capture group around both of them. That was the key. I switched them because the one that you had first will match IP addresses, so they would never get captured separately.
    – Ryan Gross
    Jul 29, 2011 at 23:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.