PHP Regex: group must end with character but do not capture this character -


example regex:

/^([\w]+:)?other:(.*)$/ 

example string:

test:other:words... 

the first group match "test:", want capture "test". @ first thought:

/^([\w]+)?:?other:(.*)$/ 

but realised can't have single : in beginning. how can capture group if exists must end : : must not captured group?

example input , output:

randomstring:constantstring:somethingelse 

should give 'randomstring' first group.

and

constantstring:somethingelse 

should give first group empty

here go:

^(?:(\w+):)?constantstring:(.*)$ 

(?:) non capturing group

demo


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -