user10012
user10012

Reputation: 715

Keep RegEx Pattern From Selecting a '-' at the Beginning of a Group

I am trying to extract the portion of text inside of a shortcode that looks like this

[img:some-code-here-which-could-have-UPPER-case-and-0-9-numbers]. 

Questions A: Will this ALWAYS working the way I intend? B: How can I not select any text that starts with a dash?

\[img:{1}[a-zA-Z\-0-9]+\]

My issue is this regex seems to work for 99% of what I want but the it still selects the following which it should not.

[img:-not-a-code]

Upvotes: 0

Views: 44

Answers (1)

Guile
Guile

Reputation: 1494

I would write this

\[img:[a-zA-Z0-9][a-zA-Z0-9\-]*\]

But this matches with [img:x-------]. Don't know if it fits your need ?

In order to prevent more than one dash between words you could try

\[img:[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*\]

or

\[img:\w+(\-\w+)*\]

Upvotes: 1

Related Questions