REGEX:抓住所有东西,直到一个特定的单词 [英] REGEX: Grabbing everything until a specific word

查看:96
本文介绍了REGEX:抓住所有东西,直到一个特定的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< code>< a>< a>< strike>这里的示例数据< / strike>< / a> $ b

我希望a标签内的所有内容都可以结束

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

< a> 标签,但如果有?



我想知道您是否可以告诉它抓取所有 [^< / a>] 而不是 [^<]



使用 /< a> ;(。*)< \ / a> / 无法正常工作。有时我会在< a> 标记中获取所有内容,有时我会收到包含在该调用中的大量行。 (。*?)< \ / a> /
< / code><

应该可以工作。这个?使它变得懒惰,所以它在匹配< / a> 部分之前尽可能少地抓取。但使用。将意味着它匹配所有内容,直到找到< / a> 。如果你希望能够跨越线条进行匹配,你可以使用 preg_match

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

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


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.

解决方案

/<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

/<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

这篇关于REGEX:抓住所有东西,直到一个特定的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆