0

i have a regex in my php code that should match every number and all "operators" +-*/^r() my regex looks like this

/(?:(\d+(?:\.\d+)|(\+)|(-)|(\*)|(\/)|(\^)|(r)|(\()|(\))))/

and when tested with the string preg_match_all($expression,"2+2",$results) it gives me back

Array
(
    [0] => Array
        (
            [0] => '+'
        )

    [1] => Array
        (
            [0] => ''
        )

    [2] => Array
        (
            [0] => '+'
        )

    [3] => Array
        (
           [0] => ''
        )

    [4] => Array
        (
            [0] => ''
    )

    [5] => Array
        (
            [0] => ''
        )

    [6] => Array
        (
            [0] => ''
        )

    [7] => Array
        (
            [0] => ''
        )

    [8] => Array
        (    
            [0] => ''
        )

    [9] => Array
        (
            [0] => ''
        )

)

When (if it works right) i should be getting this

Array
(
    [0] => Array
        (
            [0] => '2'
            [1] => '+'
            [2] => '2'
        )

    [1] => Array
        (
            [0] => '2'
            [1] => ''
            [2] => '2'
        )

    [2] => Array
        (
            [0] => ''
            [1] => '+'
            [2] = > ''
        )

    [3] => Array
        (
           [0] => ''
           [1] => ''
           [2] => ''
        )

    [4] => Array
        (
           [0] => ''
           [1] => ''
           [2] => ''
        )
    ...

)

Note, it returns similar behaviour for all of the operators, it seems to ignore the numbers entirely

Note, I DO need separate capture groups so i have different match indexes for each result

5
  • 1
    What is the goal of this? Commented Jul 12, 2013 at 14:55
  • "should match every number and all "operators" +-*/^r()" i am trying to tokenize based on this regex Commented Jul 12, 2013 at 14:59
  • Then you don't need to use separated capture groups Commented Jul 12, 2013 at 15:00
  • sorry, somewhat new to regex, you mean rather than my current i should use what instead? maybe post an answer rather than a comment Commented Jul 12, 2013 at 15:02
  • @casimiretHippolyte sorry, upon looking into it, i do need separate capture groups, because i need a 2D array separated on the indexes of each result, i need to take a different action depending on the token and i'd rather match strings once and be done Commented Jul 12, 2013 at 15:14

1 Answer 1

1

you can simply do this:

$pattern = '~\d+(?:\.\d+)?|[-+*/^r()]~';
Sign up to request clarification or add additional context in comments.

2 Comments

dont the -^() all need to be escaped?
@Nanos: You don't need to escape these characters inside a character class. ~ is the pattern delimiter (as / or #)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.