161

I have this regex here;

\[sometag\](.*)\[/sometag\]

Which is supposed to catch text surrounded by the [sometag] tag. It works for single line information contained in these tags, like on the string [sometag]this is a bit of text[/sometag]. But it doesn't work on text that spans multiple lines, like this;

[sometag] here is more text

it spans more than one line [/sometag]

For some reason, Sublime text's regex finder won't recognize the tags across multiple lines. I want to know if this a problem with Sublime Text, a toggleable option, or just my personal incompetence with regexes.

2 Answers 2

306

At the start, use a dotall modifier (?s) to make dot to match also newline characters.

(?s)\[sometag\](.*?)\[\/sometag\]

DEMO

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

5 Comments

Thanks! Why do I have to include those extra question marks though?
? after * will do a non-greedy match. Consider [b]foo[/b]foo[b]bar[/b] as example. \[b\].*\[\/b\] would match the whole from the starting b to the last closing b.
just play with adding and removing the ? in this regex101.com/r/gI0xC3/2
(?s) causes the dot to also include newlines
thansk god you exist. Ive always tried to do that in sublime but I could not because I though we could not use modifiers in sublime search/replace
22

If modifying of dot's mode is inadmissible for some reasons, you may take that:

[sometag](.|\n)+?[/sometag]

2 Comments

Never use (.|\n)+? if you have an access to the DOTALL modifier. If you cannot use (?s) in some falvor, use [\s\S]/[\d\D]/[\w\W], but not the alternation patter suggested here. This pattern is highly inefficient and can easily lead to a timeout issue.
@WiktorStribiżew: Can you explain why it is inefficient? I always use [\s\S], but at a glance (.|\n) would seem equivalent? Is it to do with matching groups? But then (?:.|\n) would be the same?

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.