473,378 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

REGEX: Quotes still captured, don't want them

I am trying to strip out the contents of all double-quoted phrases in a
string. I tried the following:

preg_match_all("/(?:\").*?(?:\")/i", $theString, $matches,
PREG_PATTERN_ORDER);

Given the following input string, whose quotes are escaped (slashed):

dogs \"bean bags\" cats \"in quotes\"

I get these two matches:

"bean bags\"
"in quotes\"

As you can see, I'm still capturing the quotes and one, but curiously not
both, of the slashes.

I want to get just the contents of the string inside the slashed
double-quotes, so here are my questions:

1) How can I get just the contents of the string inside the slashed
double-quotes?

2) Why are the double-quotes still being captured despite my use of
non-capturing parentheses?

3) Why did only one of the slashes get captured?

Thanks

Jul 31 '05 #1
3 2621
Robert Oschler wrote:
1) How can I get just the contents of the string inside the slashed
double-quotes?

2) Why are the double-quotes still being captured despite my use of
non-capturing parentheses?

The (?:...) syntax does capture the specified character sequences; what you
are really after are assertions:

'/(?<=\\\")([^\"]+)[^\s](?=\\\")/i'

Mind the [^\s]; I have put it there to distinguish between \"bean bags\" and
\" cats \", which is also captured as a quoted substring otherwise.
3) Why did only one of the slashes get captured?


Because you didn't mark the backslashes as literal characters, as in:

'/(?:\\\").*?(?:\\\")/i' (Also note the single quotes enclosing the pattern)
JW

Jul 31 '05 #2

"Janwillem Borleffs" <jw@jwscripts.com> wrote in message
news:42**********************@news.euronet.nl...
Robert Oschler wrote:
1) How can I get just the contents of the string inside the slashed
double-quotes?

2) Why are the double-quotes still being captured despite my use of
non-capturing parentheses?

The (?:...) syntax does capture the specified character sequences; what

you are really after are assertions:

'/(?<=\\\")([^\"]+)[^\s](?=\\\")/i'

Mind the [^\s]; I have put it there to distinguish between \"bean bags\" and \" cats \", which is also captured as a quoted substring otherwise.
3) Why did only one of the slashes get captured?

Because you didn't mark the backslashes as literal characters, as in:

'/(?:\\\").*?(?:\\\")/i' (Also note the single quotes enclosing the

pattern)

JW


Janwillem,

Thanks! That does it. Another chapter in the neverending battle between
the characters we use to program in, being the same characters we have to
manipulate in text strings. :)

Robert
Aug 1 '05 #3
Robert Oschler wrote:
I am trying to strip out the contents of all double-quoted phrases in a
string. I tried the following:

preg_match_all("/(?:\").*?(?:\")/i", $theString, $matches,
PREG_PATTERN_ORDER);

Given the following input string, whose quotes are escaped (slashed):

dogs \"bean bags\" cats \"in quotes\" ....
I want to get just the contents of the string inside the slashed
double-quotes, so here are my questions:


preg_match_all('/(?:\\\\")(.*?)(?:\\\\")/i', $theString, $matches,
PREG_PATTERN_ORDER);

Use $matches[1];
Aug 1 '05 #4

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
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...
6
by: Dave | last post by:
I'm struggling with something that should be fairly simple. I just don't know the regext syntax very well, unfortunately. I'd like to parse words out of what is basically a boolean search...
1
by: msnews.microsoft.com | last post by:
Hi, I have the expression "<font+>""(*)""</font>+\?AUTHOR_ID=+"">(*)</a>" Any body can tell me what is the meaning of that expression and what is the output of the expression. Regards, Muhammad...
4
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...
5
by: skavan | last post by:
Hi, I'm just wrapping my head around regex and am pretty sure it can do the task at hand - but it's too complex for my brain to process -- so am throwing it out there for you experts to comment...
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
8
by: bonit99 | last post by:
hello! I have a question on how I can replace all occurences of one special character within a matching string? for example string: key = 'som#ething#something' OR anotherkey <= 'anot#her'...
4
by: pedrito | last post by:
I have a regex question and it never occurred to me to ask here, until I saw Jesse Houwing's quick response to Phil for his Regex question. I have some filenames that I'm trying to parse out of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.