473,397 Members | 1,961 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,397 software developers and data experts.

Capturing substrings with preg_match_all()

Hi,

I'm experiencing a problem when trying to capture substrings with
preg_match_all() from strings like

"set('Hello','World')"

using the following Regular Expression (PERL syntax):

"/^set\((?:'([^']+)'(?(?!\)),))+\)$/"

The resulting matches array looks like this:

Array
(
[0] =Array
(
[0] =set('Hello','World')
)

[1] =Array
(
[0] =World
)

)

I wonder why "Hello" is not in the matches array. Did I do something
wrong in my Regular Expression?

Thanks in advance.
Ingmar

Jun 5 '07 #1
1 1927
On 05.06.2007 17:18 ngmr80 wrote:
Hi,

I'm experiencing a problem when trying to capture substrings with
preg_match_all() from strings like

"set('Hello','World')"

using the following Regular Expression (PERL syntax):

"/^set\((?:'([^']+)'(?(?!\)),))+\)$/"

The resulting matches array looks like this:

Array
(
[0] =Array
(
[0] =set('Hello','World')
)

[1] =Array
(
[0] =World
)

)

I wonder why "Hello" is not in the matches array. Did I do something
wrong in my Regular Expression?

Thanks in advance.
Ingmar
Hi Ingmar

grouping is not automatic, to capture N strings you must have N groups
in your expression or use preg_match_all, for example:

$re1 = '/set\( (.*?) \)/x';
$re2 = "/' (?: \\\\. | [^'] )* ' /x";

preg_match($re1, "set('Hello','Wo\'rld')", $m);
preg_match_all($re2, $m[1], $m);
print_r($m);


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Jun 5 '07 #2

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

Similar topics

2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&" preg_match_all ("|product=(\d{5,10})&|i", $html, $out); $out...
3
by: Han | last post by:
I know this is possible (because preg can do almost anything!), but can't get a handle on the syntax. I have an HTML string: <font size="3"><a...
2
by: Han | last post by:
The following pattern (which is one subpattern in a string of several) looks for the following $xxx,xxx.xx (with the dollar sign) or xxx,xxx.xx (space in replace of missing dollar sign) ...
0
by: petrovitch | last post by:
While using the following loop to extract images from the google search engine I discovered that preg_match_all works much faster parsing small strings in a loop than extracting all of the urls at...
10
by: dave | last post by:
Hi I have succesfully captured the users login detail, but I now have a new problem I have assigned the login to a Session Variable, however when I come to use the string stored in the variable it...
9
by: C3 | last post by:
I have to process some data in C that is given to me as a char * array. I have a fairly large number of substrings (well, they're not actually printable, but let's treat them as strings) that I...
10
by: greatprovider | last post by:
i'm starting with a string such as "Na**3C**6H**5O**7*2H**20" im attempting to match all **\d+ ...once i can match all the double asterix \d i intend to wrap the \d in "<sub>" tags for display...
8
by: girish | last post by:
Hi, I want to generate all non-empty substrings of a string of length >=2. Also, each substring is to be paired with 'string - substring' part and vice versa. Thus, gives me , , , , , ] etc....
2
loriann
by: loriann | last post by:
hi, I have a problem with preg_match function returning empty arrays for my wonderful regexes. However, I can't see what I am doing wrong - maybe one of you could help? I'm loading the source...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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 project—planning, coding, testing,...

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.