Search code examples
phpregexpreg-matchpreg-match-all

Regex Match string


This should be a nice easy one for a regex expert.

I have a group name <APP>_<Resource>_<Action> the action segment contains a combination or CRUD. There are 13 possible combinations that these can appear in.

CRUD, CRU, CRD, CUD, RUD, CR, CU, CD, RU, RD, UD, C, R, U, D

I just want a regex to match on of those patterns within the action segment of the group name. The application and resources can change to various different strings.

Example Group Names

PM_folder1_cru, PM_folder2_ud, PM_folder3_cr, PM_folder4_cu, PM_personalFolder_crud

Thanks in advance

EDIT

This is currently done using:

$allowedActions = ['CRUD', 'CRU', 'CRD', 'CUD', 'RUD', 'CR', 'CU', 'CD', 'RU', 'RD', 'UD', 'C', 'R', 'U', 'D'];

if (in_array(strtoupper($action), $allowedActions)) {

Solution

  • I think something like this would work:

    ^C?R?U?D?$
    

    To avoid matching empty strings, you can use a lookahead assertion:

    ^(?!$)C?R?U?D?$