Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex whitespace

Tags:

regex

php

preg_match('/<div class="prices">/s<h3>(.+?)<\/h3>/is', $response, $matches);

There's whitespace and potentially new lines between the prices div and the h3 tag. How do I use /s to match that?

like image 278
Webnet Avatar asked Mar 19 '11 18:03

Webnet


People also ask

What is whitespace in regex?

\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.

How do you check for whitespace in regex?

The RegExp \s Metacharacter in JavaScript is used to find the whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is same as [ \t\n\r].

What does \\ mean in regular expression?

You also need to use regex \\ to match "\" (back-slash). 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.


1 Answers

You don't use /s, you use \s*.

  • It's a backslash (\), not a slash (/).
  • The * afterwards means that it matches zero or more whitespace characters.

Also, please consider using an HTML parser if you are trying to find HTML tags. A proper HTML parser will be able to correctly handle whitespace, HTML comments and other features of HTML that your regular expression cannot handle.

like image 154
Mark Byers Avatar answered Sep 20 '22 11:09

Mark Byers