PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > Regex with removing double quotes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Regex with removing double quotes

Réponse
 
LinkBack Outils de la discussion
Vieux 05/11/2007, 19h11   #1
Schroeder, AJ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Regex with removing double quotes

Hello group,

I am attempting to remove double quotes from the beginning and ending of a
string. Admittedly, I am not the best with regular expressions, but I do
have two that work with preg_replace(). Here they are:

$pattern = '[^"]'; <--- removes the beginning double quote
$pattern = '["$]'; <--- removes the ending double quote

But when I try to make this all one statement with:

$pattern = '[^"].*["$]';

PHP throws a warning with an unknown modifier '.'. I then tried to enclose
'.*' in parentheses and I get an unknown modifier '('.

This is getting beyond my regex knowledge, if anyone has any advice on this
it would be greatly appreciated.

Thanks in advance,

AJ Schroeder


  Réponse avec citation
Vieux 05/11/2007, 19h35   #2
Justin Koivisto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

Schroeder, AJ wrote:
> Hello group,
>
> I am attempting to remove double quotes from the beginning and ending of a
> string. Admittedly, I am not the best with regular expressions, but I do
> have two that work with preg_replace(). Here they are:
>
> $pattern = '[^"]'; <--- removes the beginning double quote
> $pattern = '["$]'; <--- removes the ending double quote
>
> But when I try to make this all one statement with:
>
> $pattern = '[^"].*["$]';
>
> PHP throws a warning with an unknown modifier '.'. I then tried to enclose
> '.*' in parentheses and I get an unknown modifier '('.
>
> This is getting beyond my regex knowledge, if anyone has any advice on this
> it would be greatly appreciated.
>
> Thanks in advance,
>
> AJ Schroeder
>
>


when using preg_* you need escape chars for the expression....

your first patterns should look something like:

$pat = '`^"`'; // quote at beginning of string

$pat = '`"$`'; // quote at end of string

$pat = '`^"(.*)"$`'; // single-line string that starts and ends with a quote

--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 05/11/2007, 20h40   #3
Schroeder, AJ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

Justin Koivisto wrote:
> Schroeder, AJ wrote:
>> Hello group,
>>
>> I am attempting to remove double quotes from the beginning and
>> ending of a string. Admittedly, I am not the best with regular
>> expressions, but I do have two that work with preg_replace(). Here
>> they are: $pattern = '[^"]'; <--- removes the beginning double quote
>> $pattern = '["$]'; <--- removes the ending double quote
>>
>> But when I try to make this all one statement with:
>>
>> $pattern = '[^"].*["$]';
>>
>> PHP throws a warning with an unknown modifier '.'. I then tried to
>> enclose '.*' in parentheses and I get an unknown modifier '('.
>>
>> This is getting beyond my regex knowledge, if anyone has any advice
>> on this it would be greatly appreciated.
>>
>> Thanks in advance,
>>
>> AJ Schroeder
>>
>>

>
> when using preg_* you need escape chars for the expression....
>
> your first patterns should look something like:
>
> $pat = '`^"`'; // quote at beginning of string
>
> $pat = '`"$`'; // quote at end of string
>
> $pat = '`^"(.*)"$`'; // single-line string that starts and ends with
> a quote


Hmm, maybe I am that dense, but that last expression seemed to torch the
entire string and return nothing. The first two work no problem.

Still confused...


  Réponse avec citation
Vieux 05/11/2007, 20h56   #4
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

On Mon, 05 Nov 2007 20:11:49 +0100, Schroeder, AJ <aj1@qg.com> wrote:

> Hello group,
>
> I am attempting to remove double quotes from the beginning and ending of
> a
> string. Admittedly, I am not the best with regular expressions, but I do
> have two that work with preg_replace(). Here they are:
>
> $pattern = '[^"]'; <--- removes the beginning double quote
> $pattern = '["$]'; <--- removes the ending double quote
>
> But when I try to make this all one statement with:
>
> $pattern = '[^"].*["$]';
>
> PHP throws a warning with an unknown modifier '.'. I then tried to
> enclose
> '.*' in parentheses and I get an unknown modifier '('.


Yes indeed.
The preg_* function expect a pattern like this:
(delimiter)(pattern)(delimiter)(modifiers)

You are free to choose your own delimiter ('/' is pretty standard), and a
second occurance of that character is assumed to end the pattern and start
the modifiers. (I'm actually quite ammazed it matches '[' to ']', a quite
rigid implementation would throw an error stating that " was an unknown
modifier).

$string = preg_replace('/(^"+|"+$)/','',$string);
--
Rik Wasmus
  Réponse avec citation
Vieux 06/11/2007, 02h22   #5
Justin Koivisto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

Schroeder, AJ wrote:
> Justin Koivisto wrote:
>> Schroeder, AJ wrote:
>>> Hello group,
>>>
>>> I am attempting to remove double quotes from the beginning and
>>> ending of a string. Admittedly, I am not the best with regular
>>> expressions, but I do have two that work with preg_replace(). Here
>>> they are: $pattern = '[^"]'; <--- removes the beginning double quote
>>> $pattern = '["$]'; <--- removes the ending double quote
>>>
>>> But when I try to make this all one statement with:
>>>
>>> $pattern = '[^"].*["$]';
>>>
>>> PHP throws a warning with an unknown modifier '.'. I then tried to
>>> enclose '.*' in parentheses and I get an unknown modifier '('.
>>>
>>> This is getting beyond my regex knowledge, if anyone has any advice
>>> on this it would be greatly appreciated.
>>>
>>> Thanks in advance,
>>>
>>> AJ Schroeder
>>>
>>>

>> when using preg_* you need escape chars for the expression....
>>
>> your first patterns should look something like:
>>
>> $pat = '`^"`'; // quote at beginning of string
>>
>> $pat = '`"$`'; // quote at end of string
>>
>> $pat = '`^"(.*)"$`'; // single-line string that starts and ends with
>> a quote

>
> Hmm, maybe I am that dense, but that last expression seemed to torch the
> entire string and return nothing. The first two work no problem.
>
> Still confused...


The last pattern assumes the string is a single line (no \r or \n in
it), and both begins and ends with "

This would match that pattern:
"The fox jumped over the lazy dog."

This would not:
The fox jumped over the "lazy" dog.

If you want to match as in the second string, try something more along
the lines of:

$pat = '`"([^"]*)"`';

When you are unsure how your patterns are matching, try something like
this to view it quickly:

if(preg_match_all($pat,$string,$m)){
echo '<pre>';
print_r($m);
echo '</pre>';
}


see the manual page for preg_match_all if you are not sure what the $m
array means.

--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 06/11/2007, 02h23   #6
Justin Koivisto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

Schroeder, AJ wrote:
> Justin Koivisto wrote:
>> Schroeder, AJ wrote:
>>> Hello group,
>>>
>>> I am attempting to remove double quotes from the beginning and
>>> ending of a string. Admittedly, I am not the best with regular
>>> expressions, but I do have two that work with preg_replace(). Here
>>> they are: $pattern = '[^"]'; <--- removes the beginning double quote
>>> $pattern = '["$]'; <--- removes the ending double quote
>>>
>>> But when I try to make this all one statement with:
>>>
>>> $pattern = '[^"].*["$]';
>>>
>>> PHP throws a warning with an unknown modifier '.'. I then tried to
>>> enclose '.*' in parentheses and I get an unknown modifier '('.
>>>
>>> This is getting beyond my regex knowledge, if anyone has any advice
>>> on this it would be greatly appreciated.
>>>
>>> Thanks in advance,
>>>
>>> AJ Schroeder
>>>
>>>

>> when using preg_* you need escape chars for the expression....
>>
>> your first patterns should look something like:
>>
>> $pat = '`^"`'; // quote at beginning of string
>>
>> $pat = '`"$`'; // quote at end of string
>>
>> $pat = '`^"(.*)"$`'; // single-line string that starts and ends with
>> a quote

>
> Hmm, maybe I am that dense, but that last expression seemed to torch the
> entire string and return nothing. The first two work no problem.
>
> Still confused...


The last pattern assumes the string is a single line (no \r or \n in
it), and both begins and ends with "

This would match that pattern:
"The fox jumped over the lazy dog."

This would not:
The fox jumped over the "lazy" dog.

If you want to match as in the second string, try something more along
the lines of:

$pat = '`"([^"]*)"`';

When you are unsure how your patterns are matching, try something like
this to view it quickly:

if(preg_match_all($pat,$string,$m)){
echo '<pre>';
print_r($m);
echo '</pre>';
}


see the manual page for preg_match_all if you are not sure what the $m
array means.

--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 06/11/2007, 20h53   #7
Schroeder, AJ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Regex with removing double quotes

Rik Wasmus wrote:
> On Mon, 05 Nov 2007 20:11:49 +0100, Schroeder, AJ <aj1@qg.com> wrote:
>
>> Hello group,
>>
>> I am attempting to remove double quotes from the beginning and
>> ending of a
>> string. Admittedly, I am not the best with regular expressions, but
>> I do have two that work with preg_replace(). Here they are:
>>
>> $pattern = '[^"]'; <--- removes the beginning double quote
>> $pattern = '["$]'; <--- removes the ending double quote
>>
>> But when I try to make this all one statement with:
>>
>> $pattern = '[^"].*["$]';
>>
>> PHP throws a warning with an unknown modifier '.'. I then tried to
>> enclose
>> '.*' in parentheses and I get an unknown modifier '('.

>
> Yes indeed.
> The preg_* function expect a pattern like this:
> (delimiter)(pattern)(delimiter)(modifiers)
>
> You are free to choose your own delimiter ('/' is pretty standard),
> and a second occurance of that character is assumed to end the
> pattern and start the modifiers. (I'm actually quite ammazed it
> matches '[' to ']', a quite rigid implementation would throw an error
> stating that " was an unknown modifier).
>
> $string = preg_replace('/(^"+|"+$)/','',$string);


Justin and Rik,

Thank you for the ! I am now able to strip off beginning and ending
quotes of strings. Although I am still messing around with different strings
to handle all the combination of strings I will have to process, I have a
great starting point.

Thank you again.

AJ Schroeder


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h22.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19526 seconds with 15 queries