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 #179339 is a reply to message #179335] Mon, 08 October 2012 12:49 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sun, 07 Oct 2012 21:20:21 -0700, Scott Johnson wrote:

> I have two possible values I need to match as the same.
>
> Name/11 (This one is pulled from the source DB)
> Name/11* (This is what I need to match to with or without the (*))
>
> The pattern base name is pulled from a DB so I need to find a way to do
> this pragmatically.
>
> Now some of the names have a forward slash / so I am using hash mark #
> as a delimiter.
>
> I was able to find the "Name/11" using
>
> #^Name/11#
>
> or more specifically;
>
> $value = "Name/11";
> $pattern = "#^" . $value . "#";
>
> 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.

I suspect that this may be the php parser - when it parses a string
literal declared in double quotes as:

$str = " blah blah \<something> blah blah "

then when the string is read, the \<something> gets converted to a
character, ie the "\" escape character is processed when php parses the
string, and hence is no longer there when the string is used by the regex
function.

If you want to include a \ character to escape the * meta character in
the stored string that gets passed to the preg function, I think you have
two choices:

Single quotes:

$str = ' blah blah \* blah blah '

Escape the escape:

$str = " blah blah \\* blah blah "

Working out the "right" combination of single or double quotes, single or
double escaping etc etc can be a nightmare!

There is another alternative that you might be able to use, which is to
put the * in a single character class of its own, as inside a character
class, * is not a meta character:

$str = " blah blah [*] blah blah "

Rgds

Denis McMahon
[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 09:15:19 GMT 2024

Total time taken to generate the page: 0.05413 seconds