1

I have a string:

tel:+13172221234;foo=1234;bar=000-000-000;wombat=mydomain.com

Note that the string always begins with tel:+{{some-number}}. I want to capture so that I have the following:

+13172221234
foo
1234
bar
000-000-000
wombat
mydomain.com

What I have is this: ([^?=&;]+)=([^;]*) which returns three matches, each with a variable and a value (i.e., foo/1234). However I cannot seem to capture the number in front. I would think that simply putting a tel:(\w+) in front would do it but it doesn't work.

https://regex101.com/r/cM2pQ5/2

Anyone able to help? Maybe I should just do two separate regexes?

1
  • 1
    Which programming language you were using Commented Oct 2, 2015 at 11:00

4 Answers 4

4

You can use this lookahead based regex:

([^:?=&;]+)(?=[=;])(?:=([^;]*))?

RegEx Breakup:

(             # Start of capture group #1
   [^:?=&;]+  # Match 1 or more char of anything but ? or = or & or ;
)             # End of capture group #1
(?=           # Start of positive lookahead
   [=;]       # to assert next position is either = or ;
)             # End of positive lookahead
(?:           # Start of "optional" non-capturing group
   =          # match literal =
   (          # Start of capture group #2
      [^;]*   # match 0 or more of any char that is not a ;
   )          # End of capture group #2
)?            # Start of non-capturing group. ? in the end makes it optional

RegEx Demo

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

6 Comments

You don't match the tel, is it intended?
Right because tel is not in the expected output list of OP.
This worked for me, thanks. Not exactly sure what it's doing ;) I'm going to have to dig into lookaheads.
Glad you liked it but a bit surprised to see the answer you've accepted. You never mentioned in question that your input will always have tel: are line start as I didn't take that liberty in building my regex here.
The author above explained how they got the answer. That's important for me and other people who come across this post in the future. I still don't have a decent explanation of your regex here.
|
2

As you can face or = or : as a Key/Value separator I suggest this:

([^?=&;]+)(=|:)([^;]*)

4 Comments

you can face or = or : as a key/value pair -- could you explain that? This works, although I don't need the : or = to turn up in the results.
@Jeff your key is the first part: Tel, Foo, Bar. The Value is, well, the value it has... Like in a Dictionary
Oh, you meant I can use = or : as kv separator. I was confused about the word face.
@Jeff : sorry, my English is still under construction :p
1

Here's one more way: (regex101)

^tel:([^;]+)|;([^=]+)=([^;]+)

This has two parts - the first part only matches the start of the string and the second part matches the rest of the key value pairs. I'm assuming that your string starts with tel:

Comments

1

I would simply match this:

[^;=]+

Comments

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.