How to make this regular expression work?

I have a little problem I want to find in

<tr><td>3</td><td>foo</td><td>2</td>

foo, I use:

$<tr><td>\d</td><td>(.*)</td>$

to find foo, but it does not work, because it does not match </td>at the end of foo, but with </td>at the end of the line

+3
source share
3 answers

You have to make .*lazy, not greedy. Read more about lazy vs greedy here .
Your end of string anchors ( $) also doesn't make sense. Try:

<tr><td>\d<\/td><td>(.*?)<\/td>

(As seen on rubular .)

. HTML. , , XML- (, ). " " - .

+2

:

^<tr><td>\d</td><td>(.*?)</td>

( , regex xml)

0

$ ^.

If you do not want to match all the way to the end of the line, do not use $at the end. However, since *greedy, he will snap as much as possible. Some regex implementations have a non-greedy version that will work, but you probably just want to change (.*)to ([^<]*).

0
source

Source: https://habr.com/ru/post/1762719/


All Articles