Asked  7 Months ago    Answers:  5   Viewed   27 times

I'm trying to get all substrings matched with a multiplier:

$list = '1,2,3,4';
preg_match_all('|d+(,d+)*|', $list, $matches);
print_r($matches);

This example returns, as expected, the last match in [1]:

Array
(
    [0] => Array
        (
            [0] => 1,2,3,4
        )

    [1] => Array
        (
            [0] => ,4
        )

)

However, I would like to get all strings matched by (,d+), to get something like:

Array
(
    [0] => ,2
    [1] => ,3
    [2] => ,4
)

Is there a way to do this with a single function such as preg_match_all()?

 Answers

78

According to Kobi (see comments above):

PHP has no support for captures of the same group

Therefore this question has no solution.

Wednesday, March 31, 2021
 
Freddie
answered 7 Months ago
65

You can use:

preg_match_all("!<span[^>]+>(.*?)</span>!", $str, $matches);

Then your text will be inside the first capture group (as seen on rubular)

With that out of the way, note that regex shouldn't be used to parse HTML. You will be better off using an XML parser, unless it's something really, really simple.

Saturday, May 29, 2021
 
freeMagee
answered 5 Months ago
87

This regex will do the trick:

(d+)d (d+)h (d+)m (d+)s

Each value (day, hour, minute, second) will be captured in a group.

About your regex: I don't know what do you mean by "isn't correct", but I guess it's probably failing because your regex is greedy instead of lazy (more info). Try using lazy operators, or using more specific matches (d instead of ., for example).

EDIT:

I need them to be separate variables

After matching, they will be put in different locations in the resulting array. Just assign them to variables. Check out an example here.

If you have trouble understanding the resulting array structure, you may want to use the PREG_SET_ORDER flag when calling preg_match_all (more information here).

Saturday, May 29, 2021
 
relyt
answered 5 Months ago
76

You could try the below regex which uses positive lookahead assertion.

(?=(bw+ Road d+b)|(bd+ suiteb))

DEMO

String s = "XYZ Road 123 Suite";
Matcher m = Pattern.compile("(?i)(?=(\b\w+ Road \d+\b)|(\b\d+ suite))").matcher(s);
while(m.find())
{
    if(m.group(1) != null) System.out.println(m.group(1));
    if(m.group(2) != null) System.out.println(m.group(2));
}

Output:

XYZ Road 123
123 Suite
Friday, August 20, 2021
 
Andy Fedoroff
answered 2 Months ago
95

NOTE AngularJs 1.3 now supports Getter/Setter for ng-model. Refer to http://docs.angularjs.org/api/ng/directive/ngModelOptions for more information.


I could break the infinite loop with extra calls to

ngModelCtrl.$setViewValue()

and

ngModelCtrl.$render()

in the event handlers. Not sure if it's the best way to do it though.

See fiddle: http://jsfiddle.net/BDyAs/12/

EDIT:

I improved the code even more in

http://jsfiddle.net/BDyAs/15/

by separating the directive in separate ones for the getter and the setter.

Saturday, October 9, 2021
 
Joegramming
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :