Regex matching whole line starting with an exclamation mark

You can use the .+ regex with the global flag to match separate whole lines. Here's an example:

enter image description here


If you want to add an exception for <br> you should use a positive look-ahead operator. Please also note that parsing an HTML document using regex is usually not a good thing to do.

I think you should consider <br /> also. So, final reg-ex would be:

^!.+?(?=\<br\>|\<br\s*\/\>|$)

It will match highlighted portions of the sample below:

!spoiler sample
no spoiler
!spoiler
!spoiler with more <br> words
!spoiler upto new <br /> line

spoiler in the !middle of sentence

Sample: https://regex101.com/r/nZ0oY9/3


You just need

^!.*

See the updated regex demo

The ^ matches the start of a line (in Ruby), ! will match a literal ! and .* will match zero or more characters other than a newline (if you are using Ruby, which I assume from your use of the rubular Web site).

If you are using a regex flavor other than Ruby, like JS, or PHP, or .NET, you need to specify the /m - MULTILINE - modifier (e.g. /^!.*/gm in JavaScript).


If you want to match everything to the end of the line:

/(!.+)/

If you want to make sure that it follows the format !word---:

/!\w.+/

Tags:

Ruby

Regex