REGEX:抓取所有内容直到一个特定的词

时间:2021-06-03 07:28:54

ex: <a><strike>example data in here</strike></a>

例: 示例数据

I want everything inside the a tag, to the end

我要标签里的所有东西,直到最后

/<a>([^<]*)<\/a>/

It works when there are no additional tags within the <a> tag, but what if there are?

标记中没有其他标记时,它可以工作,但是如果有呢?

I want to know if you can tell it to grab everything up to [^</a>] instead of [^<] only.

我想知道如果你可以让它抓住一切[^ < / >),而不是(^ <)。

Doing it with /<a>(.*)<\/a>/ doesn't work well. Sometimes I get everything in the <a> tag and other times I get tons of lines included in that call.

用/< >(.*)<\/ >/不是很好。有时我得到标记中的所有内容,而有时我得到包含在该调用中的大量行。

1 个解决方案

#1


78  

/<a>(.*?)<\/a>/

should work. The ? makes it lazy, so it grabs as little as possible before matching the </a> part. but using . will mean that it matches everything until it finds </a>. If you want to be able to match across lines, you can use the following if with preg_match

应该工作。的吗?使它变得懒惰,所以它在匹配部分之前尽可能少地抓取。但使用。将意味着它匹配所有内容,直到找到。如果希望能够跨行匹配,可以使用preg_match

/<a>(.*?)<\/a>/s

The "s" at the end puts the regular expression in "single line" mode, which means the . character matches all characters including new lines. See other useful modifiers

最后的“s”将正则表达式放在“单行”模式中,这意味着。字符匹配所有字符,包括新行。看到其他有用的修饰符

#1


78  

/<a>(.*?)<\/a>/

should work. The ? makes it lazy, so it grabs as little as possible before matching the </a> part. but using . will mean that it matches everything until it finds </a>. If you want to be able to match across lines, you can use the following if with preg_match

应该工作。的吗?使它变得懒惰,所以它在匹配部分之前尽可能少地抓取。但使用。将意味着它匹配所有内容,直到找到。如果希望能够跨行匹配,可以使用preg_match

/<a>(.*?)<\/a>/s

The "s" at the end puts the regular expression in "single line" mode, which means the . character matches all characters including new lines. See other useful modifiers

最后的“s”将正则表达式放在“单行”模式中,这意味着。字符匹配所有字符,包括新行。看到其他有用的修饰符