Hello, please sign in or register
You are here: Home

Regular expression match strings in code

Strings in source code are commonly wrapped in single or double quotes. Escape characters are used to precede these characters if the string literally contains the string.

The regular expression pattern:

([\'|"])[^\1\\]*?(?:\\.[^\1\\]*?)*?\1

This uses:

  1. Back Referencing e.g. (firstpattern) ... \1
  2. Subpatterns e.g. (?:\\.[^\1\\]*?)
  3. Quantifier minizer e.g. *?

For more information see http://www.regular-expressions.info/examplesprogrammer.html

PHP Example: Please note the tripple backslashes "\\\"

// heredoc or nowdoc "<<<'EOF'" see http://uk.php.net/language.types.string
$s = <<<EOF

\$e = "words \" and stuff's ";

function test()
{
	echo "Test";
}

EOF;

/**
 * Match the string "words \" and stuff's "
 */

$pattern = '#([\'|"])[^\1\\\]*?(?:\\\.[^\1\\\]*?)*?\1#';
preg_match_all( $pattern, $s, $matches );
print_r( $matches );

 

Comments

Nice
Created 16/09/09
0921maoqiuyun
cheap nba jerseys, http://www.nbajerseys.net/ jordan shoes, http:/...
Created 21/09/15
guoyanying
But the replica watches sentry reebok shoes knew abercrombie and fitch uk his
Created 07/10/15
1015maoqiuyun
coach outlet online, http://www.coachoutletonline-store.us.com/
Created 15/10/15
wq123
This hilfiger outlet time swarovski jewelry something really timberland outlet strange, ...
Created 30/06/16
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
cc
adidas yeezy boost Cheap Jordans For Sale tiffany and co jewe...
Created 26/10/16
hxy1220
hxy1220 ray ban wayfarer rolex watches,rolex watches,swiss watches,watches for men,watches for women,omega watches,replica ...
Created 20/12/16
hxy1.22
hxy1.22 ugg outlet burberry outlet louis vuitton
Created 22/01/17
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share