Det tycker jag!I'm fixing the line break with this:(.*)\s*(.*)But sometimes the HTML ends in a tag on the same line: Det tyckeg jag" />

When doing a search I want to skip all code that ends in a closing tag

html regex

47 просмотра

2 ответа

I have HTML that looks like this:

 <td class="danish"> Det
         tycker jag!</td>

I'm fixing the line break with this:

<td class="danish">(.*)

But sometimes the HTML ends in a tag on the same line:

 <td class="danish">Det tyckeg jag</td>

I want it to skip lines like these when searching and find the next broken line.

In case anyone thinks it's just a frivolous thing to make the code look good, the rest of the code looks like this (not required reading):

<td class="danish"> Det
  tycker jag!</td>
  <?php audioButton("../../audio//det_lyder_godt","det_lyder_godt"); ?>

I ultimately have to take the text in the table and replace the one in the audiobutton a thousand times, but that's a different problem

Автор: Moogal Источник Размещён: 08.11.2019 11:22

Ответы (2)

1 плюс


I think this is what you're looking for:

(<td class="danish">(?:(?!</td>).)*)\r?\n\s*

This matches from <td class="danish"> to the next newline, unless there's a </td> tag first. Replace with "$1 " or "\1 " (without the quotes).

Using \r?\n instead of a literal newline makes the regex more robust. Even better is \R, if your regex flavor supports it.

Автор: Alan Moore Размещён: 20.08.2016 05:33

1 плюс

\s means "any white-space character", which includes spaces and new lines. You could explicitly search for lines that must contain a new line, by using something like:

<td class="danish">(.*)\n\s*(.*)</td>

Note the additional \n in the regex.

Автор: Tom Lord Размещён: 20.08.2016 03:09
Вопросы из категории :