Regular Expressions 101

Community Library Entry

0

Regular Expression
PCRE (PHP <7.3)

/
^\/([a-zA-Z]{2}-[a-zA-Z]{2})\/cms\/api\/preview([a-zA-Z0-9-_=\/]*)(.*?)(?(3)([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: language (which becomes QS) Group 2: second part of the URL to be maintained Group 3: Question mark "?" which needs to be removed Group 4: Query strings which needs to be maintained

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

Transformation: /preview/$2?lang=$1&$4

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

Submitted by anonymous - 6 years ago