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
Switch to threaded view of this topic Create a new topic Submit Reply
Regular Expression Help [message #179335] Mon, 08 October 2012 04:20 Go to next message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
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.

Thanks
Re: Regular Expression Help [message #179337 is a reply to message #179335] Mon, 08 October 2012 10:03 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
On Oct 8, 5:20 am, Scott Johnson <nooneh...@chalupasworld.com> 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.
Safer than this is to use preg_quote().

>
> 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.*#
The middle one will sepcifically match "Name/11*" but it will not
match "Name/11".
Since ? means "match 0 or 1 occurrences", "Name/11\*?" will match what
you want. However, it will also match "Name/11e". To match precisely
what you have described, you need something like this:
$value = "Name/11\*?";
$match_value1 = "Name/11";
$match_value2 = "Name/11*";
$match_value3 = "Name/11t";
$pattern = "#^$value$#";
echo preg_match($pattern, $match_value1);
echo preg_match($pattern, $match_value2);
echo preg_match($pattern, $match_value3);
Re: Regular Expression Help [message #179339 is a reply to message #179335] Mon, 08 October 2012 12:49 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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
Re: Regular Expression Help [message #179340 is a reply to message #179335] Mon, 08 October 2012 12:59 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
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 (*))

Hold on a cotton picking moment.

Why are you even trying to use regexp for this? Just because regex can
compare strings does not mean all string compares must be done with regex.

Could you not use a string compare?

if ( strncmp( $something_from_db, $something_to_match_against, $n ) === 0
) {
// strings match for first $n characters!
}

Rgds

Denis McMahon
Re: Regular Expression Help [message #179343 is a reply to message #179335] Mon, 08 October 2012 17:39 Go to previous messageGo to next message
BootNic is currently offline  BootNic
Messages: 10
Registered: November 2010
Karma: 0
Junior Member
In article <k4tkan$ea$1(at)dont-email(dot)me>, Scott Johnson
<noonehome(at)chalupasworld(dot)com> 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.

http://www.regular-expressions.info/characters.html

\*

http://www.regular-expressions.info/charclass.html

[*]

> Can someone please explain the best way to escape an optional (*) from
> a string. Or even point me to a good source.

#Name/11(?:[*])?#

http://www.regular-expressions.info/brackets.html

[snip]



--
BootNic Mon Oct 8, 2012 01:39 pm
Genius is eternal patience.
*Michelangelo*
Re: Regular Expression Help [message #179347 is a reply to message #179335] Tue, 09 October 2012 10:29 Go to previous messageGo to next message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma: 0
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
--
Re: Regular Expression Help [message #179348 is a reply to message #179335] Wed, 10 October 2012 10:38 Go to previous message
Scott Johnson is currently offline  Scott Johnson
Messages: 196
Registered: January 2012
Karma: 0
Senior Member
On 10/7/2012 9:20 PM, 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.
>
> Thanks
>
>
Thank you to everyone for taking the time and providing input.

It was both a combination of using the proper pattern and the
peculiarities with PHP and making it not munge my pattern.

Again thanks, you pulled me thru.

Scotty
  Switch to threaded view of this topic Create a new topic Submit Reply
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 23:19:55 GMT 2024

Total time taken to generate the page: 0.02204 seconds