C
C
camperofcamper2022-01-17 12:39:42
Regular Expressions
camperofcamper, 2022-01-17 12:39:42

How to write a regular expression for a string in which the first part of the string is known, but the rest is unknown?

Follow the link to regex101.com for test data. I need to pull out the contents of tags that contain a certain number <tr>inside . <td class="num">1</td>By reference - this is 1.

I understand how easy it is to find the right td - this is also done by reference. The problem for me is to throw in the regular expression all the rest of the contents of the tr tag. What is the best way to do this? Really it is necessary to register a template for each following line? Plus, I don’t quite understand what to do if the source code is without tabs: if you remove the tabs in the source code, then the regular expression stops working

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VicTHOR, 2022-01-17
@camperofcamper

https://regex101.com/r/D2hGbD/2

S
Stalker_RED, 2022-01-17
@Stalker_RED

<td class="num">(.*?)<\/td>
https://regex101.com/r/a1ygUX/1
It's worth noting that parsing html with regular expressions is a rather dumb task. On simple examples, it seems to work, but there is a rather big risk that everything will fall apart at any moment. Well, for example, one more class or attribute will be added to this num.
And regex parsing just can't be made reliable, because HTML is context-free, not a regular language.
https://stackoverflow.com/a/1732454

V
Viktor Taran, 2022-01-17
@shambler81

https://regex101.com/r/D7CIBS/1
exactly according to TK;)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question