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

3

6

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.

Feucht answered 27/5, 2016 at 11:34 Comment(3)
Gedit 3.10.4 in Ubuntu 14.04Feucht
Doesn't ^(.*?)\bdefault\b --> \1rwd work? I think lazy matching here is a much better solution than a tempered greedy token suggested in the answer below.Rego
Or you can try \bdefault\b(.*) and replace with rwd\1 (similar wiktors answer)Annates
D
11

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

Diversify answered 27/5, 2016 at 11:52 Comment(7)
In geany this works fine, but in Gedit, it adds some newlines creating blank lines in between.Feucht
It is really strange. The pattern does not match a newline at all (if the DOTALL option is OFF in GEdit).Rego
Ok, got it, probably it is on, I will check and confirm tomorrow, thanks.Feucht
No it's not working, I just tried this in Geany, it's replacing all occurrences of word default to rwd, but I just want to replace first occurrence of the ord default. It's not complying to my requirements.Feucht
The first occurrence on a line or in the whole string? In Notepad++, it works so that the first occurrence on a line is replaced. Actually, .*? is equivalent of the tempered gredy token suggested by anubhava. Just my expression should be faster.Rego
Yes so it is contradictory, in demo you have /gm as regex flags but it doesn't work if multiline mode is enabled. In geany when I turned multiline off it did work.Feucht
@VickyDev There is nothing contradictory if you mean the demo at regex101.com. m stands for MULTILINE option that makes ^ match start of any line and $ match end of any line. This mode is enabled by default in text editors, you never need to specify the m flag there. I added GEdit and Geany screenshots proving my solution works.Rego
L
2

You can search using this lookahead regex:

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

And replace it using:

\1rwd

RegEx Demo

Litalitany answered 27/5, 2016 at 11:40 Comment(4)
Have you tried this in Gedit, it doesn't work in that.Feucht
I don't have gedit but checked online that gedit allows lookaheadLitalitany
Ok, so I have to check how to enable lookahead in Gedit. Will do that tomorrow in office.Feucht
It's working in other editors like Geany, Notepad++ etc., so +1 for that.Feucht
M
0
        var path = @"C:\Users\pcNameHere\Downloads\dictionary.csv";
        var destination = @"C:\Users\pcNameHere\Downloads\dictionaryEnglish.csv";
        var database = File.ReadAllLines(path);
        var pattern = ",";
        Regex reg = new Regex(pattern);
        string[] result = new string[database.Length];

        for (int i = 0; i < database.Length; i++)
        {
            result[i] = reg.Replace(database[i], "|", 2);
        }
        File.WriteAllLines(destination, result);

Here is my sample for anyone looking in the future. I had the English dictionary text file with lines like these:

"Abacus","n.","A table or tray strewn with sand, anciently used for drawing, calculating, etc."

And I had to replace comma delimiters with something else so I could import them into a database, because there were commas inside the word definitions and the database would treat them as new columns. This code changed the first two occurrences of a comma in each line in the text file.

Medovich answered 27/6, 2018 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.