Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match all characters and new lines?

Tags:

regex

php

Just ran into a bit of an issue with my regex. I would like to match all content inside the square bracket tags.

Here is my regex: /(?:\[.*])(.*)(?:\[\/.*])/

I am running it against this content:

[TITLE]More Things[/TITLE]
[BODY]This is not working
still not working
nope, hasn't magically started working

I'd like to match double carriage returns[/BODY]

It will only match the single line tags at the moment. Any help is appreciated, thanks.

like image 995
ToXic73 Avatar asked Jul 01 '15 06:07

ToXic73


People also ask

How do you match a new line character in regex?

"\n" matches a newline character.

What is \r and \n in regex?

Regex recognizes common escape sequences such as \n for newline, \t for tab, \r for carriage-return, \nnn for a up to 3-digit octal number, \xhh for a two-digit hex code, \uhhhh for a 4-digit Unicode, \uhhhhhhhh for a 8-digit Unicode.

What is regex multiline?

Multiline option, or the m inline option, enables the regular expression engine to handle an input string that consists of multiple lines. It changes the interpretation of the ^ and $ language elements so that they match the beginning and end of a line, instead of the beginning and end of the input string.

What is difference [] and () in regex?

This answer is not useful. Show activity on this post. [] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.


1 Answers

If you're on php, you just need to add s modifier. And also it's better to capture the opening tag and refer it while writing pattern for the closing tag.

/(?:\[([^\]]*)\])(.*?)(?:\[\/\1\])/s

If you're on javascript then you must need to turn each . in the above regex to [\S\s]

DEMO

like image 187
Avinash Raj Avatar answered Oct 05 '22 03:10

Avinash Raj