473,625 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regex help 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
Nov 5 '07 #1
6 5728
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

Nov 5 '07 #2
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...
Nov 5 '07 #3
On Mon, 05 Nov 2007 20:11:49 +0100, Schroeder, AJ <aj*@qg.comwrot e:
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)(pat tern)(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
Nov 5 '07 #4
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_a ll($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

Nov 6 '07 #5
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_a ll($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

Nov 6 '07 #6
Rik Wasmus wrote:
On Mon, 05 Nov 2007 20:11:49 +0100, Schroeder, AJ <aj*@qg.comwrot e:
>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)(pat tern)(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 help! 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
Nov 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4493
by: Robert Oschler | last post by:
Can someone give me a regex expression that will split a sentence containing words and double-quoted phrases, into an array? I don't want the words between the double-quotes to be split using the space (or comma) character as a delimiter. I can do one or the other, tokenize the words or tokenize the double-quoted strings, but I can't figure out how to combine the two into the same regex expression. Note: I *do* want to capture (retain)...
3
2334
by: Craig Kenisston | last post by:
I have the sudden need to split a text that may have any of the following tokens : Words with quotes or double quotes. Words with no quotes at all. Numbers with and without decimal points, no commas allowed, but may contain parenthesis which I would like to keep apart to drop later. They may be separated by comas, spaces or semicolon.
4
728
by: William Stacey [MVP] | last post by:
Would like help with a (I think) a common regex split example. Thanks for your example in advance. Cheers! Source Data Example: one "two three" four Optional, but would also like to ignore pairs of brackets like: "one" <tab> "two three" ( four "five six" ) Want fields like:
4
3198
by: JS | last post by:
I am writing a C# app that needs to parse a sentence entered by the user for a simple boolean search. I need to capture all of the AND words that are not inside of double quotes. However, I am having a heck of a time figuring out a regex for it. Can anyone assist with a regex to find all the AND's not in double quotes? An example sentence might be: red and blue and "crazy elephant" and "orange and red" and stuff.
7
1414
by: Fuzzyman | last post by:
Hello all, I'm writing a module that takes user input as strings and (effectively) translates them to function calls with arguments and keyword arguments.to pass a list I use a sort of 'list constructor' - so the syntax looks a bit like : checkname(arg1, "arg 2", 'arg 3', keywarg="value", keywarg2='value2', default=list("val1", 'val2'))
9
2079
by: jmchadha | last post by:
I have got the following html: "something in html ... etc.. city1... etc... <a class="font1" href="city1.html" onclick="etc."click for <b>info</bon city1 </a> ... some html. city1.. can repeat lot of times here.... Requirement: ------------------- I want to get the value of "href" i.e "city1.html" by searching "city1" between the <a</atag. Please note that "city1" can repeat lot of
5
1402
by: Bragadiru | last post by:
Hi, I'm using the following Regex to parse for method parameters. It works if there are no spaces between commas. How can I change the regex to support method calls like : MyMethod('uno', 'due','tres' , 'quatro' ) Regex(@"+",RegexOptions.Compiled); // Parse for function params (...,...,...) Thanks for any advice
6
2560
by: =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?= | last post by:
Hi, I need to replace double quotes inside the text from the database with " to correctly display my text. I was trying to use Regex to perform such a task: Regex.Replace(text, "", """) to catch standard " sign and non-standard “ and ” - but non-standard double quotes are not recognised by Replace function. Can anyone suggest the correct expression to use (I was trying to avoid multiple String.Replace functions that work fine - I...
3
6091
by: eBob.com | last post by:
Is there a regex pattern which will match a VB.Net string? I.E. a regex which matches ... "this is a ""vb.net"" string" (I don't want three matches in this case, I want one.) I've come up with various solutions for the two-double-quotes-in-a-row problem but none of them have worked out. Thanks, Bob
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8692
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8497
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6116
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.