tags:

views:

97

answers:

4

I am trying to print "1" if there are at least two of the same figure in the match, else 0.

What is wrong in the regex?

if ( max ( array_map ('strlen', preg_split('/([0-9])[^0-9]*\1/', "1 2 3 1 4") ) ) == 1 )
        echo 1;
else
        echo 0;
A: 

The [^0-9]* matches any number of NON-digit characters. So if there's another number, it will fail the match. Try replacing [^0-9]* with a simple .*, which will match digits or non-digits.

Robert Fraser
+2  A: 
echo preg_match('/(?<=^|[^0-9])([0-9)+)(?=[^0-9]).*(?<=[^0-9])\1(?=[^0-9]|$)/', "1 2 3 1 4");

Will match for any repeated number in the sequence, and echo 1 if there is something repeated, 0 if not.

(Original version just looked for something repeated after each other, this matches repeated anywhere in the string)

Mez
Your command gives me "0" although it should give "1". I am not sure where the bug is in your command.
Masi
If the input is always as shown (i.e. space seperated), it's much simpler to use `\b` instead of all that lookbehind/lookahead noise. (The only time `\b` would fail is if alphas or underscore are used as delimiters.)
Peter Boughton
This can then give a simpler expression of `\b([0-9]++).*?\b\1\b`
Peter Boughton
hey, sorry, seems PHP doesnt class the start of a line as not being a number :DI've amended my answer
Mez
@Mez: Your regez still has the same bug. I modified your answer sligthly in trying to understand it. --- @Peter: Your regex should apparently have only one +. Your regex to Mez's example gives 0 which is surprising. - I am not completely sure about the definition of ´\b´.
Masi
Aha, Masi, that's cause of the \1 - If you change the double quotes to single quotes, it works (about to do this)
Mez
This input "65|19|24|56|85|11|13|14|53|6|60" should return zero. Your regex returns 1. What should be changed in the regex to get the feature?
Masi
Are the items always space seperated?
Mez
Mez: They are not always separated by spaces.
Masi
Oh, wait, I see...
Mez
Peter's regex seems to work well too.
Masi
A: 

Try the following code. It should print 1 when there is a repeat number.

if(0 == strlen(preg_replace('/.*([0-9]).+\1.*/', '', '1 2 3 1 5 4')))
     echo 1;
else echo 0;

The regex /.*([0-9]).+\1.*/ will match a number and another number (with .+ or anything in between them).

Hope this helps.

NawaMan
doesn't take into account 2 digit numbers
Mez
A: 

Try a lookahead assertion. I use "The Regex Coach" whenever I'm trying to figure something out. It doesn't give you hints or anything, but it does give immediate feedback.

Test string: "1 2 3 1 4 3"
Regex: ([0-9])(?=.*\1)

Basically the ()'s around the [0-9] store the result, and the lookahead (?= matches .* - any character and then \1 - what was matched first (so it looks for any number and then looks ahead to see if that number occurs again)

This will match both "1" and "3"

I'm not quite sure if php supports lookaheads, but that's my take.

JakeTheSnake
it will also match1 2 3 4 13
Mez
match against *
Mez