Home > front end >  Regex find a specific character zero or one or multiple time in a string
Regex find a specific character zero or one or multiple time in a string

Time:03-11

I'm upgrading a Symfony app with VSCode and I have numerous occurences of this kind of string :

@Template("Area:Local:delete.html.twig")

or

@Template("Group:add.html.twig")

In this case, I want to replace all the : with / to have :

@Template("Area/Local/delete.html.twig")

I can't think of doing it manually, so I was looking for a regular expression for a search/replace in the editor.

I've been toying with this fearsome beast without luck (i'm really dumb in regexp) : @Template\("([:]*)" @Template\("(.*?)" @Template\("[a-zA-Z.-]{0,}[:]")

Still, I think there should be a "simple" regexp for this kind of standard replacement.

Anyone has any clue ? Thank you for any hint

CodePudding user response:

You can use this regex with a capture group: (@Template.*):.

And replace with this $1/.

enter image description here

But you'll have to use replace all until there's no : left, that won't take long.

Just explaining a lit bit more, everything between the parenthesis is a capture group that we can reference later in replace field, if we had (tem)(pla)te, $1 would be tem and $2 would be pla

CodePudding user response:

Regex!

You can use this regex @Template\("(.[^\(\:]*)?(?:\:)(.[^\(\:]*)?(?:\:)?(.[^\(\:]*)?"\) and replacement would simply be @Template\("$1/$2/$3

You can test it out at https://regex101.com/r/VfZHFa/2

Explanation: The linked site will give a better explanation than I can write here, and has test cases you can use.

  • Related