REGEX: Grabbing everything until a specific word


Question

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
31
11/22/2009 9:18:54 AM

Accepted Answer

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

78
3/15/2012 2:11:42 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon