Forum Moderators: phranque

Message Too Old, No Replies

match all combinations, regex

         

phparion

1:03 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$str = "a1dd b1ss c2sss";

i want to write a pattern that matches all occurrences of an alphabet from a-c and then a digit (1-2) i.e a set of

a1 b1 c2, a1 b1, a1 c2, b1 c2, a1,b1,c2

I thought to write something like

preg_match("/^(a-c)\d\s(a-c)\d\s(a-c)\d/",$str,$matches);

but this logic does not work at all.

any idea?

thank you

coopster

2:23 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



For PHP, or for use in an Apache directive?

jdMorgan

2:42 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"Lowercase letter a through c, followed by digit 1 or 2"
[a-c][12]

Jim

phparion

2:49 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Array
(
[0] => Array
(
[0] => a1
[1] => b2
[2] => c2
)

)

it does not give me my desired set.

@using in php

jdMorgan

3:16 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did not mention an array. The pattern I show uses alternate-character groups, and can be used like this:

preg_match("/^[a-c][12]/",$str,$matches);

Your description of the required match-string did not match your original pattern construction, so again, this pattern matches only one letter a, b, or c, followed by one digit, 1 or 2. Since it does not have an end-anchor, *any* additional characters may follow. Modify/expand/end-anchor it to suit your actual requirement, which seems to include spaces and multiple letter/number sequences.

Jim

phparion

3:30 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i have been trying many different patterns but with no luck. the closest one, for me, was

/(\s[a-c\d]\s)+/

or

/(\s?[a-c\d]\s?)+/

in my explanation,

preceded or ended on a whitespace character with one alphabet from a to c followed by a digit, with one or more occurrences.

therefore i thought to post it here and see if some experienced person can give me the exact regex :) because i already tried many things with no luck, now i am curious to see the actual working regex for this.

g1smd

3:56 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something like this?

((\ [a-c][12])+\ )

phparion

4:16 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<?php

$str = "a1sd b2fd c2kj";
$regex = '/((\ [a-c][12])+\ )/';
preg_match($regex,$str,$matches);

echo "<pre>"; print_r($matches);
?>

returns empty resultant array means no matches.

jdMorgan

4:20 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't include the \d in the alternate character set unless you don't care about the order of characters and numbers. Also, the initial optional space can be omitted from the parenthesized sub-expression:

/\s?([a-c]\d\s?)+/

"\s" and "\ " should be equivalent, as should "[0-9]" and "\d" so that's largely a matter of 'style'.

Jim

jdMorgan

4:36 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Added: When you report that this or that pattern does not work, please provide the 'test string' that does or does not match that pattern. Otherwise, it's impossible for us to follow along and offer anything of much use in diagnosing the problem.

Jim

phparion

4:55 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



thank you for your replies J,

I have been giving the test string in my posts. I used your suggested pattern as follow

<?php

$str = "a1sd b2fd c2kj"; //testing on this string
$regex = '/\s?([a-c]\d\s?)+/';
preg_match($regex,$str,$matches);

echo "<pre>"; print_r($matches);
?>

now it gives me following output

Array
(
[0] => a1
[1] => a1
)
EDIT: the same pattern, surprisingly, gives me following array on the online RE tester,

a1
b2
c2

still not what i am looking for. i want all the combination set of alphabet-followed by a digit, in all combinations as explained in my very first post.

coopster

5:09 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So you are attempting to match the pattern in the string first, and then create combination sets of two or more unique strings based on the individual strings that were matched?

g1smd

5:15 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** a1 b1 c2, a1 b1, a1 c2, b1 c2, a1,b1,c2 ***

How many test strings is that?

phparion

5:16 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



yes, exactly. the idea is basically from the simple example

preg_match('/^(\w+)\s(\w+)/',"web master",$matches);

the $matches array will be now,

web master
web
master

similarly, i want to parse the string

a1xx b2xx c2xx and get all combination e.g

a1 b2 c2
a1 b2
a1 c2
b2 c2
a1
b2
c2

jdMorgan

5:17 pm on Mar 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I inadvertently removed the outer parentheses, which is why you got "a1" and "a1" instead of "a1 b2 c2"

$regex = '/(\s?([a-c]\d\s?)+)/';

Be careful when using on-line testers: The 'results' must be interpreted carefully for meaning, and they often vary in what their display format 'means'. Use very simple test patterns and test strings until you are sure what the display actually means.

Jim