0

I have got this string:

tcp://10.0.0.1:11211?rep=12&timeout=12

so I tried with:

(tcp://.+):([0-9]+)\?.*\&+timeout=([0-9]+).*

I need to retrieve:

  • tcp://10.0.0.1 that is the server
  • 11211 that is the port
  • timeout parameter value (in this case 12)

The tcp://10.0.0.1:11211 part exists in any case and so I need it, but the query part is not mandatory, so my problem is that if ? or timeout or = don't exist the regex doesn't retrun anything.
Due to my inexperience I'm not able to solve this problem,how can I achive this result?

Regex101 Demo

3 Answers 3

4

How about creating a new capture group after the port

([a-zA-Z]{3,4}://.+?):(\d+)(.+?timeout=(\d+).?)?

It finishes up the;

tcp://10.0.0.1:11211 in 28 steps

tcp://10.0.0.1:11211?timeout=12 in 42 steps

tcp://10.0.0.1:11211?rep=12&timeout=12 in 48 steps

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

1 Comment

Doesn't capture: tcp://10.0.0.1:11211?timeout=12
0

You can make the pattern after the match optional by adding ?:

(tcp://.+):([0-9]+)(\?.*\&?timeout=([0-9]+).*)?
                   ↑      ↑                  ↑↑

See DEMO

2 Comments

Doesn't capture: tcp://10.0.0.1:11211?timeout=12
it tooks 100 steps to do it
0

The following regex seems to work for me.

(tcp://.+):([0-9]+)(\?.*\&+timeout=([0-9]+).*|)

It’s based on your regex. The part after the interrogation mark (\?.*\&+timeout=([0-9]+).*) has just been enclosed in a (...|).

It is usual to use (pattern|) if something might not exist, but you need the regex to match the whole string nonetheless.

1 Comment

Doesn't capture: tcp://10.0.0.1:11211?timeout=12

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.