65.9K
CodeProject is changing. Read more.
Home

Checking for "any character" using regular expressions in multiline text

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.93/5 (6 votes)

Jan 30, 2012

CPOL
viewsIcon

49111

".*" may not be what you want in multi-line strings

Suppose you have the string
Start this is a 
multiline string End
and you want to match everything between the "Start" and "End" using a regular expression. The obvious one to use is
Start(.*)End
but in multi-line strings (strings that contain newlines) this will fail because "." does not match against newlines. The answer? Use "[\s\S]" (match and space character or non-space character) instead of "."
Start([\s\S]*)End