FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Regular Expression Help
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Regular Expression Help [message #179347 is a reply to message #179335] Tue, 09 October 2012 10:29 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 08/10/2012 6:20, Scott Johnson escribió/wrote:
> But as for the (*), no glory.
> I originally tried
>
> #^Name/11?*#
> #^Name/11\*#
> #^Name/11.*#
>
> It must be obvious I am having issues finding a way to escape the (*)
> and am not very versed with regex.
>
> Can someone please explain the best way to escape an optional (*) from a
> string. Or even point me to a good source.
>
> I used http://www.regular-expressions.info/tutorial.html but was unable
> to find how to handle the star.
>
> And if you bing up the *, it sort of gets ignored.

You need to escape the "*" because it's special char in regular
expressions and the escape character is "\":

http://es.php.net/manual/en/regexp.reference.meta.php

And you need to escape the "\" because it's a special char in PHP strings:

http://es.php.net/manual/en/language.types.string.php

So you need this:

<?php
var_dump( preg_match('/\\*/', '*') );

.... to produce this regular expression:

/\*/

If you find it messy, you can use the built-in escaping function:

<?php
var_dump( preg_match('/' . preg_quote('*', '/') . '/', '*') );
var_dump( preg_match('#' . preg_quote('*', '#') . '#', '*') );

When unsure, always var_dump():

<?php
var_dump( '#' . preg_quote('*', '#') . '#' );

string(4) "#\*#"


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: How to run a batch file using PHP?
Next Topic: dll load problem
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 13:25:53 GMT 2024

Total time taken to generate the page: 0.05858 seconds