Regular Expressions 101

Community Patterns

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^\/cms\/api\/preview\/(.*)\/([a-zA-Z]{2}-[a-zA-Z]{2})\/([a-zA-Z0-9-_\/]*)(.*?)(?(4)([a-zA-Z0-9-_=&]*)|)$
/
mg

Description

Request: get a specific segment of URL related to Language and add it as a query string, plus stripping of first 2 segments of the incoming URL:

Group 1: first part of the URL to be maintained Group 2: language (which becomes QS) Group 3: second part of the URL to be maintained Group 4: Question mark "?" which needs to be removed Group 5: Query strings which needs to be maintained

Example: /cms/api/preview/pippo/paperino/en-US/pluto/DePaperoni/paperoga?qui=qua&quo=qui&cap-paperopoli=123

Transformation: /preview/$1/$3?lang=$2&$5

Output: /preview/pippo/paperino/pluto/DePaperoni/paperoga?lang=en-US&qui=qua&quo=qui&cap-paperopoli=123

Submitted by anonymous - 6 years ago