2

I am using Sublime Text, and I was wondering if there is a way to do this with Regex:

I have multiple files that have different lines and text.

In each of them I have a line that starts with "NAME : " and continues with the name of the person:

NAME : john doe smith

I am looking to use Regex to replace the spaces found in the text john doe smith with a character _, basically after the text NAME : , so the new line will be:

NAME : john_doe_smith

So far I have tried doing this: Matching the line that contains "NAME" (couldn't get it working with the full one "NAME : ") with Find:

^.*\b(NAME)\b.*$

But I don't know how to replace the spaces after the match.

Is there a way to accomplish this with Regex?

0

2 Answers 2

3

Depending on your regex flavour, you can do:

  • Find: (?:^NAME : |\G(?!^))\w+\K\h
  • Replace: _

A demo and explanation can be found here on Regex101.

6
  • Thank you so much! it worked flawlessly! this is amazing, thanks a lot! I've been looking for a solution for a while, this is a life saver, thank you!
    – Lernisios
    Commented Feb 8, 2022 at 9:42
  • @Lernisios: You're welcome, glad it helps.
    – Toto
    Commented Feb 8, 2022 at 9:50
  • As with my comment below, would there be a Regex syntax I can use to also make all the characters that are found after "NAME : " as lowercase? I do like the sed command, but looking to use regex as well. thank you!
    – Lernisios
    Commented Feb 8, 2022 at 22:25
  • I tried this that will convert the titlecase at the beginning of each word to lower case Find (\w+) (\w+) (\w+) (\w+) and then Replace with \L$1 $2 $3 $4 , but did not find a way to replace all characters inside a word
    – Lernisios
    Commented Feb 8, 2022 at 23:14
  • If your regex flavour supports conditonnal replacement, you can use Find: (?:^NAME : |\G(?!^))\K(\w+)(\h)? Replace: \L$1(?2_:) If it doesn't, you have to do the job in 2 psteps 1) the regx I gave in answer 2) Find ^NAME : \K.+ Replace: \L$0. Edit your question and add tag for which program/tool you're using.
    – Toto
    Commented Feb 9, 2022 at 10:25
2

It depends on where you're using the regex:

The following uses sed:

sed ':redo s/^\(\s*NAME\s*:\s*\S\+\)\s\+\([^_[:space:]]\)/\1_\2/ ;t redo' files

The trick here is the t redo, which does a conditional branch back to the label redo, to rerun the replacement until it fails.

Note that the pattern includes some glue to make spaces around the : optional, ignore trailing white space and reduce multiple spaces to a single _.

9
  • thanks for the help @Giacomo1968 I haven't used sed before, thus I tried it just now; I saw in my command line on Linux Mint that the sed command gives me the preview output, but it does not change the file itself. This is the command I ran: sed ':redo s/^\(\s*NAME\s*:\s*\S\+\)\s\+\([^_[:space:]]\)/\1_\2/ ;t redo' file1.txt Is there an extra argument to the sed command that could modify the files themselves, i.e. replace the empty spaces with _ inside the files? Also, would it be possible for me to make all the characters found after "NAME : " as like lowercase?
    – Lernisios
    Commented Feb 8, 2022 at 22:17
  • So if my input text is NAME : John Doe Smith, then the final output would look like this: NAME : john_doe_smith PS: I also tried your command to work for all the files in my folder this way, so that it applies to all txt files: sed ':redo s/^\(\s*NAME\s*:\s*\S\+\)\s\+\([^_[:space:]]\)/\1_\2/ ;t redo' *.txt
    – Lernisios
    Commented Feb 8, 2022 at 22:22
  • @darkonc would greatly appreciate your help on this, thanks!
    – Lernisios
    Commented Feb 8, 2022 at 22:50
  • the option -i or --in-place will edit the files.
    – darkonc
    Commented Feb 9, 2022 at 13:17
  • 'man sed' on the command line for more info on sed . man pages are your friend on UNIX if you haven't used them before. 'info sed'
    – darkonc
    Commented Feb 9, 2022 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .