Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #7267 > unrolled thread

Regular Expression Help

Started byScott Johnson <noonehome@chalupasworld.com>
First post2012-10-07 21:20 -0700
Last post2012-10-10 03:38 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  Regular Expression Help Scott Johnson <noonehome@chalupasworld.com> - 2012-10-07 21:20 -0700
    Re: Regular Expression Help Captain Paralytic <paul_lautman@yahoo.com> - 2012-10-08 03:03 -0700
    Re: Regular Expression Help Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-08 12:49 +0000
    Re: Regular Expression Help Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-08 12:59 +0000
    Re: Regular Expression Help BootNic <bootnic.bounce@gmail.com> - 2012-10-08 13:39 -0400
    Re: Regular Expression Help "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-10-09 12:29 +0200
    Re: Regular Expression Help Scott Johnson <noonehome@chalupasworld.com> - 2012-10-10 03:38 -0700

#7267 — Regular Expression Help

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-10-07 21:20 -0700
SubjectRegular Expression Help
Message-ID<k4tkan$ea$1@dont-email.me>
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



[toc] | [next] | [standalone]


#7269

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-10-08 03:03 -0700
Message-ID<30dbc1e3-8271-4d22-aefe-bdaf5e707832@a11g2000yqb.googlegroups.com>
In reply to#7267
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);

[toc] | [prev] | [next] | [standalone]


#7271

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-10-08 12:49 +0000
Message-ID<k4ui41$qo1$1@dont-email.me>
In reply to#7267
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

[toc] | [prev] | [next] | [standalone]


#7272

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-10-08 12:59 +0000
Message-ID<k4uimu$qo1$2@dont-email.me>
In reply to#7267
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

[toc] | [prev] | [next] | [standalone]


#7275

FromBootNic <bootnic.bounce@gmail.com>
Date2012-10-08 13:39 -0400
Message-ID<20121008133958.3af22958@bootnic.server>
In reply to#7267

[Multipart message — attachments visible in raw view] — view raw

In article <k4tkan$ea$1@dont-email.me>, Scott Johnson
<noonehome@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.

> 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.

[url] http://www.regular-expressions.info/characters.html [/url]

 \* 

[url] http://www.regular-expressions.info/charclass.html [/url]

[*]

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

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

[url] http://www.regular-expressions.info/brackets.html [/url]

[snip]



-- 
BootNic                                                Mon Oct 8, 2012 01:39 pm
  Genius is eternal patience.
  *Michelangelo*

[toc] | [prev] | [next] | [standalone]


#7279

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2012-10-09 12:29 +0200
Message-ID<k50uar$v8p$1@dont-email.me>
In reply to#7267
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
--

[toc] | [prev] | [next] | [standalone]


#7280

FromScott Johnson <noonehome@chalupasworld.com>
Date2012-10-10 03:38 -0700
Message-ID<k53j71$vse$1@dont-email.me>
In reply to#7267
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web