-2

I'm trying to find the word: <*hasburnt*> in the string below using the this regex: <\*.*(bur).*\*>

But it gives me both <*hasburnt*> <*electrical*>. How do I just get <*hasburnt*> ?

bench testedstarter, starter just makes noise, and <*hasburnt*> <*electrical*> smell.

0

1 Answer 1

2

Try this: /<.*?(bur).*?>/

Regex101 demo

The reason for ? here is because .* tries to match as much characters as possible, so it also matches <electrical. .*? makes it lazy - trying to match as little as possible, and as such ending the match at <hasburnt>.

EDIT: using ? for the first .* would make <hasburnt> independent of positions of similar strings.

Sign up to request clarification or add additional context in comments.

3 Comments

maybe even <.*?(bur).*?>, in case of <electrical> <hasburnt>
@NicolasBudig You're right, thanks. Edited.
The original string has * after the <.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.