not understanding pattern matching

  • Follow


I am wanting to change words in a string that has special tags.
Ex. "This is a %/i%special%i% word in the string."
If I want to replace any word between the %/i% and %i% what would I do?

I messed with preg_repace but I couldn't understand the pattern system.

Mike
0
Reply null5451 (36) 7/11/2012 10:35:01 PM

..oO(Michael Joel)

>I am wanting to change words in a string that has special tags.
>Ex. "This is a %/i%special%i% word in the string."
>If I want to replace any word between the %/i% and %i% what would I do?
>
>I messed with preg_repace but I couldn't understand the pattern system.

What have you tried so far? Do you have any experiences with regular
expressions?

Micha

-- 
http://mfesser.de/
Fotos | Blog | Flohmarkt
0
Reply netizen9200 (1825) 7/11/2012 10:49:40 PM


Michael Fesser wrote:

> .oO(Michael Joel)
> 
> 
>>I am wanting to change words in a string that has special tags.
>>Ex. "This is a %/i%special%i% word in the string."
>>If I want to replace any word between the %/i% and %i% what would I do?
>>
>>I messed with preg_repace but I couldn't understand the pattern system.
> 
> 
> What have you tried so far? Do you have any experiences with regular
> expressions?
> 
> Micha
> 

I have tried many things but all return errors - mostly "unknown modifier".

Mike
0
Reply null5451 (36) 7/11/2012 11:11:14 PM

Michael Fesser wrote:

> .oO(Michael Joel)
> 
> 
>>I am wanting to change words in a string that has special tags.
>>Ex. "This is a %/i%special%i% word in the string."
>>If I want to replace any word between the %/i% and %i% what would I do?
>>
>>I messed with preg_repace but I couldn't understand the pattern system.
> 
> 
> What have you tried so far? Do you have any experiences with regular
> expressions?
> 
> Micha
> 

answer to your last question - No
0
Reply null5451 (36) 7/11/2012 11:11:59 PM

..oO(Michael Joel)

>Michael Fesser wrote:
>
>> .oO(Michael Joel)
>> 
>> 
>>>I am wanting to change words in a string that has special tags.
>>>Ex. "This is a %/i%special%i% word in the string."
>>>If I want to replace any word between the %/i% and %i% what would I do?
>>>
>>>I messed with preg_repace but I couldn't understand the pattern system.
>> 
>> 
>> What have you tried so far? Do you have any experiences with regular
>> expressions?
>> 
>> Micha
>> 
>
>I have tried many things but all return errors - mostly "unknown modifier".

Which means syntax error.

Please post your code.

Micha

-- 
http://mfesser.de/
Fotos | Blog | Flohmarkt
0
Reply netizen9200 (1825) 7/11/2012 11:26:10 PM

Michael Fesser wrote:

> .oO(Michael Joel)
> 
> 
>>Michael Fesser wrote:
>>
>>
>>>.oO(Michael Joel)
>>>
>>>
>>>
>>>>I am wanting to change words in a string that has special tags.
>>>>Ex. "This is a %/i%special%i% word in the string."
>>>>If I want to replace any word between the %/i% and %i% what would I do?
>>>>
>>>>I messed with preg_repace but I couldn't understand the pattern system.
>>>
>>>
>>>What have you tried so far? Do you have any experiences with regular
>>>expressions?
>>>
>>>Micha
>>>
>>
>>I have tried many things but all return errors - mostly "unknown modifier".
> 
> 
> Which means syntax error.
> 
> Please post your code.
> 
> Micha
> 
I was trying to just first get it to replace the exact thing typed. Then 
move on to replacing any word within the %/i% and %i%

     $Scripture="this is a %/i%test%i%";
     preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);

This errors with Unknown modifier t
I tried what I thought was escaping it by putting a \test but that just 
messes up more. See I haven't a clue.

Mike
0
Reply null5451 (36) 7/11/2012 11:31:12 PM

--Sig_/HyCZvLANpVaUz1m1Qy31s3H
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: quoted-printable

In article <V5KdnVyG6ph9kWPSnZ2dnUVZ_uWdnZ2d@earthlink.com>,
Michael Joel <null@void.void> wrote:

> Michael Fesser wrote:
>>=20
>> .oO(Michael Joel)
>>>=20
>>> Michael Fesser wrote:
>>>>=20
>>>> .oO(Michael Joel)
>>>>>
>>>>> I am wanting to change words in a string that has special tags.
>>>>> Ex. "This is a %/i%special%i% word in the string."
>>>>> If I want to replace any word between the %/i% and %i% what would I d=
o?
>>>>>=20
>>>>> I messed with preg_repace but I couldn't understand the pattern syste=
m.
>>>>
>>>> What have you tried so far? Do you have any experiences with regular
>>>> expressions?
>>>=20
>>> I have tried many things but all return errors - mostly "unknown modifi=
er".
>>=20
>> Which means syntax error.
>>=20
>> Please post your code.
>
> I was trying to just first get it to replace the exact thing typed. Then=
=20
> move on to replacing any word within the %/i% and %i%
>=20
> $Scripture=3D"this is a %/i%test%i%";
> preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);

[url] http://php.net/manual/en/regexp.reference.delimiters.php [/url]

preg_replace("#%/i%test%i%#","XXXXXXXX",$Scripture);
=20
> This errors with Unknown modifier t
> I tried what I thought was escaping it by putting a \test but that just=20
> messes up more. See I haven't a clue.



--=20
BootNic                                               Wed Jul 11, 2012 09:3=
2 pm
  When I was young, I was put in a school for retarded kids for two years b=
efore
  they realized I actually had a hearing loss...and they called ME slow!
  *Kathy Buckley*

--Sig_/HyCZvLANpVaUz1m1Qy31s3H
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk/+KUQACgkQOcdbyBqMFBHQxQCdEjx1NAH5qK01lZZLUsXm7lG7
pOQAn25iqK4vBB+Lo0iZOc/39yC0o+ZZ
=moME
-----END PGP SIGNATURE-----

--Sig_/HyCZvLANpVaUz1m1Qy31s3H--

0
Reply bootnic.bounce (10) 7/12/2012 1:32:52 AM

Michael Joel wrote:
> I am wanting to change words in a string that has special tags.
> Ex. "This is a %/i%special%i% word in the string."
> If I want to replace any word between the %/i% and %i% what would I do?
> 
> I messed with preg_repace but I couldn't understand the pattern system.
> 
> Mike

Since this function seems to elude me I simply wrote a samll function to 
do what I want. I simply pass it the two needles, haystack and what I 
want to replace with.

Thanks
Mike
0
Reply null5451 (36) 7/12/2012 3:27:40 AM

On Wed, 11 Jul 2012 19:31:12 -0400, Michael Joel wrote:

> I was trying to just first get it to replace the exact thing typed. Then
> move on to replacing any word within the %/i% and %i%
> 
>      $Scripture="this is a %/i%test%i%";
>      preg_replace("%/i%test%i%","XXXXXXXX",$Scripture);
> 
> This errors with Unknown modifier t I tried what I thought was escaping
> it by putting a \test but that just messes up more. See I haven't a
> clue.

http://php.net/manual/en/regexp.reference.delimiters.php

When you use a regex, the first character inside your quotes is taken as 
the pattern delimiter.

In your case, this is a "%" character.

The format of a regex string is:

"delimiter pattern delimiter optional-modifiers"

Hence, "%/i%t" is the pattern "/i" with the modifier "t"

Try:

preg_replace("!%/i%test%i%!","XXXXXXXX",$Scripture);

This will use the "!" character as the pattern delimiter and should, in 
this specific case, solve your problem. Generally it's a good idea to 
pick a pattern delimiter that does not have special meaning to regex and 
is not part of your data. This isn't always easy.

Rgds

Denis McMahon
0
Reply denismfmcmahon (354) 7/13/2012 3:22:07 AM

8 Replies
33 Views

(page loaded in 0.125 seconds)

Similiar Articles:













7/17/2012 10:30:15 AM


Reply: