0

So I have a regex like this -

[\w\s.<>/]* is [boom|box|cat]+[\w\s<>/]+[fun|lame][\w\s<>./]*

but this matches

<something>things/something> is brah <HELLO>loc</HELLO> suburb.

why?

1 Answer 1

3

[...] is a character class, it will match any individual character between the brackets, i.e. your [boom|box|cat] is identical to [abcotx|]. You want (boom|box|cat) and (fun|lame).

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

3 Comments

but doesn't that mean I capture those words? I don't want to capture them
Yes, they happen to capture things. You don't have to use the backreference if you don't want to.
IF you're very concerned about the capturing (for whatever reason) you can use a non-capturing group and do (?: ... ) instead of ( ... )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.