Ne0
Ne0

Reputation: 2786

Regex - Characters between deliminator

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.

Upvotes: 0

Views: 55

Answers (2)

Toto
Toto

Reputation: 91438

I'd do:

:(\d+)\b

where

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

Upvotes: 0

Derek
Derek

Reputation: 8763

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 )

Upvotes: 1

Related Questions