0

I've frames in long sting like that:

CCadad6876adDDCCdashajdhakdhadhDDCCsfjskfjsklfjDDCClskfjlsdfjDD

The frames are grouped between CC and DD. So the example above present three frames.

CCadad6876adDD
CCdashajdhakdhadhDD
CCsfjskfjsklfjDD
CClskfjlsdfjDD

I'd like to extract the frames from this long string to the form presented just above. To do this I've used expression:

CC[a-zA-Z0-9]+DD

hoping to catch first frame within the string.

However, instead of having expected CCadad6876adDD the whole string was matched. The match is right as the CCadad6876adDDCCdashajdhakdhadhDDCCsfjskfjsklfjDDCClskfjlsdfjDD start and finishes with CC and DD as well, but how to insist the regex to focus on the first frame?.

What shall I modify in RegEx to catch first frame?

user1146081
  • 153
  • 10
  • 1
    for a regex question, please always add the information which tool/programming language are you using. since there are different regex flavors. – Kent Sep 11 '14 at 09:50
  • you just need to add `?` after `+`. For more info , see the link which shows on your question. – Avinash Raj Sep 11 '14 at 09:58
  • 1
    Which language are you using ? You need to set the regex parameter to match minimal (first match found) in the language you are using .. – Pratham Sep 11 '14 at 11:28

1 Answers1

1

You missed to specify the programming language, here comes an example in PHP:

$string = <<<EOF 
CCadad6876adDD
CCdashajdhakdhadhDD
CCsfjskfjsklfjDD
CClskfjlsdfjDD
EOF;

preg_match_all('/CC.*?DD/', $string, $matches);
var_dump($matches);

Output:

array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(14) "CCadad6876adDD"
    [1]=>
    string(19) "CCdashajdhakdhadhDD"
    [2]=>
    string(16) "CCsfjskfjsklfjDD"
    [3]=>
    string(14) "CClskfjlsdfjDD"
  }
}
hek2mgl
  • 133,888
  • 21
  • 210
  • 235