[ACCEPTED]-[^/]+ explanation in htaccess-url-rewriting

Accepted answer
Score: 17

You have two basic regex constructs here

Character class

See character classes on regular-expressions.info

[...] 13 is a character class, means this construct 12 matches one character from the class (from 11 inside the square brackets).

Your class starts 10 with a ^, that gives the character class 9 a special meaning, its a negated character class ([^...]), means matches 8 anything thats not part of the class.


See quantifiers on regular-expressions.info

+ is 7 a quantifier, meaning 1 or more

Meaning of your regex

To understand 6 what this is doing you have also to take 5 the next thing into account, the $ at the 4 end. This is an anchor that matches the 3 end of the string.

See anchors on regular-expressions.info

so ([^/]+)$ matches all characters 2 at the end of the string that are not slashes.

Here 1 you can also find a basic tutorial

Score: 8

[^/] means any character not matching /.


Score: 3

That means:

Match 1 or more characters until 5 forward slash / is found

Anything in square 4 brackets [ and ] that has caret ^ at the start 3 acts has negation and hence:

[^/] means any character 2 except /

[^/]+ means 1 or more characters except 1 /

Score: 3

[any_character] is a Character Classes or Character Sets 18 charclass Ref. [^any_character] is a negated Character Classes or Character 17 Sets charclass negated Ref.

From Anchors Ref:

Remember ^ also has the meaning: The caret ^ matches the position before the first character in the string (an Anchor) when 16 not used inside a Character class.

From charclass Ref: Metacharacters Inside Character Classes:

Note 15 that the only special characters or metacharacters 14 inside a character class are the closing 13 bracket (]), the backslash (), the caret 12 (^) and the hyphen (-). The usual metacharacters 11 are normal characters inside a character 10 class, and do not need to be escaped by 9 a backslash. To search for a star or plus, use 8 [+*]. Your regex will work fine if you escape 7 the regular metacharacters inside a character 6 class, but doing so significantly reduces 5 readability.

From Repitition Ref
+ means one or more chracters.

so, [^/]+

Means 4 match any character other than /. So, it 3 will match until a / is encountered.

For ^/([uge])/([^/]+)$

  • the string should begin with /
  • followed by character u or g or e
  • followed by /
  • followed and ended by one or more any character other than /

the 2 () (round brackets) are used for : Round 1 Brackets Create a Backreference Ref

Score: 2

The expression [^/] matches any character that 3 is not the /, and the quantor + denotes that 2 the expression to the left of the quantor 1 has to appear at leat one time.

More Related questions