Regular Expressions 101

Community Patterns

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-_=&#]*)|)(?(3)([?]*)|)(?(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: empty Group 4: Fragment Group 5: Question Mark to be removed Group 6: Query strings which needs to be maintained

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

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

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

Submitted by anonymous - 6 years ago