0

I have the following URI

myapp://config/value?servers=https://192.168.251.1:8081&celldata=Y&https=Y&certificate=mylaptop.local:8080/certificate/clientcert.p12&certificatepassword=12345&allowgps=N

I wanted a nice efficient way of extracting the ports in the query string and thought I would try learning a bit of Regex in the process.

Using :(.*?)(,|&|/) is almost the desired result, but I don't want the deliminitors in the result, just the text between.

Can some please explain what I can do to achieve this?

Note: Please add an explanation to any expressions, as I've seen plenty of similar question with answers that don't have explanations.

Edit:

Expected outcome here, would be a 8081 and 8080 as the expression for extracting the port. It will be written in C# but the programming language is irrelevant, as the expression is global.

Ne0
  • 2,688
  • 3
  • 35
  • 49
  • 3
    What language are you using? What's the expected result? – Toto Aug 12 '14 at 11:21
  • 1
    Can you show what exact results you're expecting? E.g. what strings do you want in the end? – Simon Aug 12 '14 at 11:22
  • possible duplicate of [What is the best regular expression to check if a string is a valid URL?](http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url) – Braj Aug 12 '14 at 11:35
  • No it's not a duplicate. The URI I've given as an example could be any string that contains multiple URL's or server:port syntax's. – Ne0 Aug 12 '14 at 11:40

2 Answers2

1

What about:

:(\d+)

That gives:

: - a literal colon

(\d+) - \d means digit, + means 1 or more, and brackets are for grouping ( group all the digits together)

You will then have to get the value from the 1st group, index 1 (not 0 )

Derek
  • 7,615
  • 5
  • 33
  • 58
0

I'd do:

:(\d+)\b

where

\d stands for any digit one or more times
\b stands for word boundary

Toto
  • 89,455
  • 62
  • 89
  • 125