Tuesday 15 February 2011

python - Correct regex for matching EXACTLY n instances of a pattern -



python - Correct regex for matching EXACTLY n instances of a pattern -

let's have following:

1.) /some/text/here/with-dashes/010101/ 2.) /some/text/here/too/ 3.) /some/other/really-long/text/goes/here/019293847/

i want strings contains 6 / far these regex aren't working how intended to:

(/\w+[-]?\w+){6} (/\w+[-]?\w+){6,6} (/\w+[-]?\w+){,6}?

these regex matches strings more 6 / want match strings 6 /.

the right syntax 6 matches {6} in first example.

the reason regexes you're using matching more 6 occurrences of target string aren't anchored origin , end of string. if string has (say) 10 occurrences of path components, matches first six, counts.

to prepare this, utilize e.g.:

^(/\w+[-]?\w+){6}$

or allow optional closing slash:

^(/\w+[-]?\w+){6}/?$

python regex

No comments:

Post a Comment