parker.sikand
parker.sikand

Reputation: 1381

Control regex empty matches

I am trying to pull matches out of a fairly simple string. In my input string, the general pattern goes: s(definitely a number)e(maybe some characters)s...

Example Input: s1e1s2e2s3es4e4

Expected Matches Array: [{1,1},{2,2},{3,''},{4,4}]

I have tried:

/s(\d)e([\d\:]+)?/g and get [{1,1},{2,2},{3},{4,4}] which is really close, but I need the empty string in the third group.

and I naively try

/s(\d)e(.*?)?/g which gives me [{1,''},{2,''},{3,''},{4,''}]

I'd rather tell the regex to match any character instead of using the character class in the first example.

Aside from a simple working solution, an explanation of my misunderstanding would be great as well.

Upvotes: 0

Views: 75

Answers (3)

user557597
user557597

Reputation:

I think this s(\d)e(\d|) should work on your examples. I don't know what the colon was for.

Upvotes: 0

Barmar
Barmar

Reputation: 780851

Use preg_match_all with the PREG_PATTERN_ORDER option (the default). Then it won't leave out optional groups that don't match anything in the result.

Upvotes: 2

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

Try this:

$pattern = '~s(\d)e([\d\:]*?(?=s\d|:|$))~';

Now your capturing group can match an empty string and is no more optional.

I have added a lookahead to check that there is an another "s\d" after or a : or the end of the string.

EDIT: Since the "s" is not allowed in the second capturing group, you can simply write:

$pattern = '~s(\d)e([^s]*)~';

Upvotes: 1

Related Questions