Starting and ending the expression with special character

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Starting and ending the expression with special character

Post by LDusan »

Since '/' or '#' or other special characters are used to delimit regular expression, how could I write expression in which the expression itself starts and end with special characters. For example:

Code: Select all

 
preg_match_all ('/!.*?.!/',$val5,$out2);
 
This won't work, I guess because '!' is taken as delimiter instead of '/'. Any ideas?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Starting and ending the expression with special character

Post by AbraCadaver »

Depends upon what you're trying to match. I don't think you need to escape the exclamations marks.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Starting and ending the expression with special character

Post by LDusan »

I am trying to match everything that starts and ends with "!" and put it in array using preg_match_all.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Starting and ending the expression with special character

Post by pickle »

The regex engine uses the first character - so the ! is not being treated as a delimiter in this case. I think you just have your expression wrong.

Code: Select all

/(!.*!)/
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ridgerunner
Forum Contributor
Posts: 214
Joined: Sun Jul 05, 2009 10:39 pm
Location: SLC, UT

Re: Starting and ending the expression with special character

Post by ridgerunner »

LDusan wrote:Since '/' or '#' or other special characters are used to delimit regular expression, how could I write expression in which the expression itself starts and end with special characters. For example:

Code: Select all

 
preg_match_all ('/!.*?.!/',$val5,$out2);
 
This won't work, I guess because '!' is taken as delimiter instead of '/'. Any ideas?
Actually, in your regex the '!' is not the delimiter, the matching '/' chars are the delimiters. The only error with your regex is the extra dot right before the second '!' which matches any char in this position (which you don't want). Remove the second dot and your regex works just fine.

But using the lazy dot-star is not the best solution for this problem. Better is to specifically match exactly what you want (non-'!'s). Since you are looking for a string of characters that are not '!', say exactly that in the regex by using '[^!]*' rather than '.*?' like so:

Code: Select all

$count = preg_match_all ('/![^!]*!/', $subject, $matches);
Hope this helps! :)
Post Reply