-1

I am trying to write a regex which match the first tag in line with string "Apple"

Example:

<div class="tn-atom" field="tn_text_1584898828640" target="_blank">Apple</div>

I need to select this tag:

<div class="tn-atom" field="tn_text_1584898828640" target="_blank">

I can select all lines with regex ^(?!.*(Apple)).*$

But how can I select only the first tag?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

2 Answers2

0

^.*(?=(Apple)) will select everything up until the last case-insensitive instance of "Apple"

duppydodah
  • 165
  • 1
  • 3
  • 17
  • Thanks, but I do not need to select everything, but only the first tag, because there may be other tags or words. – Artem Larin Nov 24 '21 at 07:08
0

This pattern ^(?!.*(Apple)).*$ will match a line that does not contain Apple.

If there are no angle brackets in between, you can use a capture group and capture from <...> without any brackets in between followed by matching Apple.

^\s*(<([^\s<>]+)[^<>]*>)Apple<\/\2>

See a regex demo.

The value is in capture group 1.

Note that this can be an error prone solution when dealing with HTML that can contain nested elements and < > chars by themselves. If you have the availability to use a DOM parser I would advice to use that instead.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70