0

How do I get the following values ​​in Regex from the following string

<tr><td>4 <td><b><a href="number.php?v=41a&k=53&t=102">3</a></b>

v = 41
k = 45
t = 102

I wanted only the number of each segment (v k t)

I tried this and got only one at a time

v=\d+
4
  • You want to extract those values individually or separately (3 regexs)? Commented Aug 11, 2017 at 14:39
  • If possible, in a single regex I wanted to extract the three Commented Aug 11, 2017 at 14:45
  • 1
    1) use DOMDocument/DOMXPath to extract the href value. 2) use parse_url to extract the query part of the url. 3) use parse_str to get the different values. Commented Aug 11, 2017 at 14:50
  • What language are you using? Commented Aug 11, 2017 at 15:26

2 Answers 2

1

If your input string is really as simple as you said, you could use preg_match_all:

$str = '<tr><td>4 <td><b><a href="number.php?v=41a&k=53&t=102">3</a></b>';
preg_match_all('/(?<=[vkt]=)\d+/', $str, $m);
print_r($m[0]);

Output:

Array
(
    [0] => 41
    [1] => 53
    [2] => 102
)
Sign up to request clarification or add additional context in comments.

2 Comments

Better than I imagined
@André: Because it's a feature of preg_match_all, it isn't only a regex.
0

Use positive lookbehind for each letter:

(?<=v=)\d+
(?<=k=)\d+
(?<=t=)\d+

3 Comments

Because each regex is for different value, when you concatenate them, they will be useless.
@André: regexr support only javascript regexes and lookbehind is not supported by javascript regexes.
Well, it's actually a matter of opinion but generally regex101 is good.

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.