Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX: Grabbing everything until a specific word

Tags:

html

regex

php

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.

like image 556
chicken Avatar asked Sep 29 '08 00:09

chicken


People also ask

How do you match everything before a character?

A regular expression to match everything before a specific character makes use of a wildcard character and a capture group to store the matched value. Another method involves using a negated character class combined with an anchor. Let's investigate various methods of writing this.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do I match a specific word in a regular expression?

Java regex to match specific word Strictly speaking, “\b” matches in these three positions: Before the first character in the data, if the first character is a word character. After the last character in the data, if the last character is a word character.

How do you restrict in regex?

Prepare a Text question type. Go to the question's Settings. Go to Validation Criteria and choose the Manually enter your validation logic in XLSForm code option. In the Validation Code box, enter your regex formula between the quotation marks (' ') of the regex(., ' ') format.


1 Answers

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

like image 160
Kibbee Avatar answered Sep 27 '22 20:09

Kibbee