1

I am using this regex in sscanf

sscanf($seat, "%d-%[^(](%[^@]@%[^)])");

And it works well when i'm getting this kind of strings: 173-9B(AA@3.45 EUR@32H) but when i'm getting this kind of string: 173-9B(@3.14 EUR@32H)

it's all messed up, how can I also accept empty strings between the first ( and the first @ ?

Dano
  • 139
  • 1
  • 7

1 Answers1

1

You would be better off using a regex in preg_match to handle optional data presence in input:

$re = '/(\d*)-([^(]*)\(([^@]*)@([^)]*)\)/';

preg_match($re, '173-9B(@3.45 EUR@32H)', $m);
unset($m[0]);
print_r($m);

Output:

Array
(
    [1] => 173
    [2] => 9B
    [3] =>
    [4] => 3.45 EUR@32H
)

And 2nd example:

preg_match($re, '173-9B(AA@3.45 EUR@32H)', $m);
unset($m[0]);
print_r($m);

Array
(
    [1] => 173
    [2] => 9B
    [3] => AA
    [4] => 3.45 EUR@32H
)

Use of ([^@]*) will make it match 0 more characters that are not @.

anubhava
  • 761,203
  • 64
  • 569
  • 643