3

I'm trying to extract the value from this string using regex but I can't seem to get it right.

string:

?Ntt=crockett%20&%20Jones&Rdm=718&searchType=simple&type=search&Ns=&N=&Nrpp=12&No=0&Nr=AND(product.active:1,NOT(record.type:Store))

I need the value of Ntt which is crockett%20&%20Jones.

The regex I have currently is:

\?Ntt=(([&%])|([^&=]))*

but this results in:

?Ntt=crockett%20&%20Jones&Rdm

How can I update my regex to produce the value I need?

3
  • the & usually separates inputs in a query string. Are you sure you need &%20Jones? IF not, use [&\?]Ntt=([^&]*) Commented Dec 22, 2020 at 11:28
  • @mankowitz, I do need &%20Jones - that's why I'm struggling to find a suitable regex Commented Dec 22, 2020 at 11:35
  • How do you know that the second group is part of what you are trying to capture? In other words, how do you know how much you need? Commented Dec 22, 2020 at 11:41

2 Answers 2

1

You can use a capturing group based regex like

[&?]Ntt=((?:%20&%20|[^&])*)

See the regex demo.

Alternatively, you can use a lookbehind based regex like

(?<=[&?]Ntt=)(?:%20&%20|[^&])*

See the regex demo.

Details

  • (?<=[&?]Ntt=) - a positive lookbehind that matches a location that is immediately preceded with ? or & and then Ntt=
  • (?:%20&%20|[^&])* - a non-capturing group that matches zero or more occurrences of a %20&%20 substring or any char other than &.
Sign up to request clarification or add additional context in comments.

Comments

0

[&\?]Ntt=([^&]*)

First capture the separator (either ? or &), then capture the key, then capture everything until the next separator.

Demo: https://regex101.com/r/VuXQXP/1

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.