Home > Blockchain >  need to find first few characters in a line and add them to the end of the line using regex in notep
need to find first few characters in a line and add them to the end of the line using regex in notep

Time:01-31

I need to find first 2 or 3 or 4 characters from start of each line and add them to the end of the line. sample data is as below.

--sample data
    ('10','aaaaa','user_abc','user_abc'),   
    ('101','aaaaa','user_abc','user_abc'),  
    ('1001','aaaaa','user_abc','user_abc'),  

---sample data after replace should look as below
    ('10','aaaaa','user_abc','user_abc',10),
    ('101','aaaaa','user_abc','user_abc',101),
    ('1001','aaaaa','user_abc','user_abc',1001),

I am able to find first 2,3,4 characters using regex in notepad

find what: (('\d{2,4}') but not able to append/replace these values correctly at the end of the line.

CodePudding user response:

You may try the following find and replace, in regex mode:

Find:    \('(\d )'(.*?)\)(,?)
Replace: ('$1'$2,$1)$3

Demo

This regex pattern matches:

  • \( match (
  • '(\d )' match a string literal which is a number in $1
  • (.*?) then capture the rest of the tuple in $2
  • \) match )
  • (,?) match optional ending comma in $3

We then piece together the replacement, adding the number in $1 as a literal at the end of the tuple.

CodePudding user response:

To find the first few characters in a line and add them to the end of the line using a regular expression in Notepad , follow these steps:

  1. Open the file in Notepad
  2. Press CTRL H to open the "Find and Replace" dialog
  3. In the "Find what" field, enter the following regular expression: ^(.{3})(.*)$
  4. In the "Replace with" field, enter the following: $2$1
  5. Ensure the "Regular expression" option is selected in the "Search Mode" section
  6. Click the "Replace All" button

This will find the first three characters at the beginning of each line (.{3}) and capture them as a group ((.{3})). The rest of the line ((.*)) will also be captured as a separate group. The "Replace with" expression $2$1 will then rearrange the two captured groups so that the first three characters are at the end of the line.

  • Related