RegEx - extend a given string while leaving the rest untouched

46 views Asked by At

I have an associate array inside a PHP class method going like this:

// ...

$filters = [
    self::FILTER_CREATION_DATE => "Base/*/Creation/Date.php",
    self::FILTER_CREATION_DATE_BETWEEN => "Base/*/Creation/Date.php",
    self::FILTER_CREATION_DATE_GREATER => "Base/*/Creation/Date.php",
    self::FILTER_CREATION_DATE_GREATER_OR_EQUAL => "Base/*/Creation/Date.php",
    self::FILTER_CREATION_DATE_LESS => "Base/*/Creation/Date.php",
    self::FILTER_CREATION_DATE_LESS_OR_EQUAL => "Base/*/Creation/Date.php",
];

// ...

What I would like to do is to convert this string from:

self::FILTER_CREATION_DATE_BETWEEN => "Base/*/Creation/Date.php",

to this one:

self::FILTER_CREATION_DATE_BETWEEN => "Base/*/Creation/Date/Between.php",

I would like to use a RegEx to extend the string but leave the rest untouched. I need to do this because there's more than 120 constants defined ending with *_BETWEEN.

How can I do this?

1

There are 1 answers

0
PitchBlackCat On BEST ANSWER

In the Intellij editor or the free Notepad++, you can find and replace by regex. I'm sure other IDE's have similar functionality

  • Find self::([_A-Z]+)_BETWEEN => "(.*)/Date.php"(,)*
  • Replace self::$1_BETWEEN => "$2/Date/Between.php"$3

The regex groups the variable components of your search together by wrapping it in ()

In the replace you can reference them in order by $1, $2, etc..