Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot doesn't match new lines?

Tags:

regex

php

When I write a regex with . in it, it doesn't match new lines.

preg_match('/.*+?/') ...

What do I need to write, to match all possible characters, and new lines too?

like image 815
Simon Avatar asked Aug 21 '10 13:08

Simon


People also ask

Does dot match new line?

By default in most regex engines, . doesn't match newline characters, so the matching stops at the end of each logical line. If you want . to match really everything, including newlines, you need to enable "dot-matches-all" mode in your regex engine of choice (for example, add re. DOTALL flag in Python, or /s in PCRE.

How do you match a new line in regex?

"\n" matches a newline character.

What does dot match in regex?

The dot (.) matches any character. A single character that doesn't have any other special meaning matches that character. A string enclosed in brackets [ ] matches any single character from the string. Ranges of ASCII character codes may be abbreviated as in a-z0-9.

How do you match periods in regex?

The period (.) represents the wildcard character. Any character (except for the newline character) will be matched by a period in a regular expression; when you literally want a period in a regular expression you need to precede it with a backslash.


1 Answers

Add the s modifier, e.g.

'/./s'
like image 135
kennytm Avatar answered Sep 24 '22 23:09

kennytm