Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace only the first occurrence of a word with regex in text-editor

I want to replace only first occurrence of word(default) in each line with another word(rwd).

As below I want this:

../app/design/adminhtml/default/default/layout/pmodule.xml
../app/design/frontend/default/default/layout/pmodule.xml
../app/design/frontend/default/default/template/company/module/gmap.phtml

To be replaced to this:

../app/design/adminhtml/rwd/default/layout/pmodule.xml
../app/design/frontend/rwd/default/layout/pmodule.xml
../app/design/frontend/rwd/default/template/company/module/gmap.phtml

I have tried \bdefault\b but in vain.

like image 254
Vicky Dev Avatar asked Mar 13 '23 04:03

Vicky Dev


2 Answers

You can use a regex with a lazy dot matching pattern:

^(.*?)\bdefault\b

To replace with \1rwd.

See the regex demo

Pattern details:

  • ^ - start of line/string
  • (.*?) - Group 1 capturing any 0+ characters other than a newline as few as possible up to the first
  • \bdefault\b - whole word default.

GEdit screenshot:

enter image description here

Geany screenshot:

enter image description here

like image 101
Wiktor Stribiżew Avatar answered Mar 14 '23 16:03

Wiktor Stribiżew


You can search using this lookahead regex:

^((?:(?!\bdefault\b).)*)default

And replace it using:

\1rwd

RegEx Demo

like image 26
anubhava Avatar answered Mar 14 '23 18:03

anubhava