preg_match_all with (...)*

Posted on 2004-10-08
Last Modified: 2008-03-06
When I use preg_match_all to get repeated items, such as:

preg_match_all('/([A-Z]{3})(-([A-Z]{2})-([A-Z]{3}))*/', $myinput, $results);

it overwrites the results each time it iterates through the (...)* section.  How can I preserve these, so that when I input a string like "AAA-BB-CCC-DD-EEE" it will give me out items AAA, BB, CCC, DD, EEE?  Right now, it just gives me out AAA, DD, and EEE.

Thanks for any help.
Question by:jimstar
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Accepted Solution

hernst42 earned 200 total points
ID: 12265331
preg_match_all can not do what you want.

It matches the whole string AAA-BB-CCC-DD-EEE.
$result[0][0] contains it. What you have to do with the result is:
$parts = explode('-', $result[0][0]);
The $parts contain whatyou want.

the subpaterns ( ) only hold the last matched items, not all possible matches
LVL 10

Assisted Solution

eeBlueShadow earned 50 total points
ID: 12265768
From (the paragraph above the BACK REFERENCES heading):

> When a capturing subpattern is repeated, the value captured is the substring that matched the final iteration. For example,
> after (tweedle[dume]{3}\s*)+ has matched "tweedledum tweedledee" the value of the captured substring is "tweedledee".

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question