Elfy
Elfy

Reputation: 1863

Match sub-strings that start with a character

I want to match parts of a string that start with a certain character (asterisk):

abc*DEFxyz => *DEF

abc*DE*Fxyz => *DE, *F

Tried preg_match_all('/[$\*A-Z]+/', $string, $matches); But it doesn't seem to work. I get *DE*F on the 2nd example

Upvotes: 0

Views: 55

Answers (2)

Screenack
Screenack

Reputation: 757

Try:

^[^*]*\*

which says "from the start of the line, skip over all non-asterisk characters and stop at the first"

Extending this:

s/^[^*]*\*(.*)/

Will return the remainder of the string after the asterisk. To include the asterisk, adjust like this

s/^[^*]*(\*.*)/

Here's a great tool for checking your regex: http://gskinner.com/RegExr/

Hope this helps

Upvotes: 1

DhruvPathak
DhruvPathak

Reputation: 43235

Change your regex to this : \*[A-Z]+

http://regexr.com?34itc

Your regex here : [$\*A-Z]+ means a string containing * and A-Z characters, not mentioning anything about start.

Upvotes: 4

Related Questions