W
W
Warog2018-04-07 16:47:25
Regular Expressions
Warog, 2018-04-07 16:47:25

How to get value from tag?

You need to get the value of the parameter from the tag.
For example:

<post id = x1>
//неизвестное кол-во записей (тегов и т.д.)
<put id=y2 max=MX1>
//неизвестное кол-во записей (тегов и т.д.)
</post>
<post id = x2>
//неизвестное кол-во записей (тегов и т.д.)
<put id=y2 max=MX2>
//неизвестное кол-во записей (тегов и т.д.)
</post>

You need to get the value of the max parameter, which is located in the post tag with id = x2.
I tried to make using "backward looking" and "forward looking", but due to the fact that "backward looking" has a limited length, nothing happened.
How can I do that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
forspamonly2, 2018-04-08
@forspamonly2

Matching hierarchical structures with regexps is quite a chore, and not every regexp engine can recursively regexps. I assume that in your case these post tags are not nested.
the easiest way is to match in two steps: first, with the simplest regexp, pull out one block with the post, and then with the same primitive regexp in the pulled out part, find the first matching attribute.
compared to one tricky regexp that does everything at once, this will work faster, and much easier / clearer for those who have to maintain this code.
and if you still need exactly one regexp, then it can look something like this:
Of course, this is a simplified example, you will have to at least handle other tags and attributes, whitespaces and so on. but there is the place where the construction with the noncapturing group greedily matches where the point with the negative lookahead is probably what you asked about.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question