Domanda

My string will be like following

'popular-game-show-identifier-popular-games-mv-11'

In above string I want to check,

  1. Each word must followed by hyphen, except first word.
  2. In between the string identifier word must exist.
  3. End of the string must contain -mv- appended to digit.

To check 1, 3 following regular expression working fine.

/[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*(-mv-(\d+))$/

Now I want to check 2 also, for that I'm modified above expression to

/[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*(\-identifier\-)(\-[a-zA-Z0-9]+)*(-mv-(\d+))$/

preg_match() always returning 0 if I use above regular expression to check all 3 conditions. I'm not getting where I've did wrong.

È stato utile?

Soluzione

You have "extra" hyphen

/[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*(\-identifier\-)(\-[a-zA-Z0-9]+)*(-mv-(\d+))$/

Should be

/[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*(\-identifier)(\-[a-zA-Z0-9]+)*(-mv-(\d+))$/
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top