Home > database >  Replace part of a string using Notepad and regex
Replace part of a string using Notepad and regex

Time:02-21

So I have hundreds of strings like this:

<div >19 avr. 2020 03:56:26 PM</div><div >John
<div >04 juin 2020 07:47:06 PM</div><div >Clint
<div >27 juil. 2020 06:05:06 PM</div><div >John
<div >05 nov. 2020 08:50:32 AM</div><div >Clint
etc..

I want them to be like this:

<div style="color:#0000FF">19 avr. 2020 03:56:26 PM</div><div >John
<div style="color:#008040">04 juin 2020 07:47:06 PM</div><div >Clint
<div style="color:#0000FF">27 juil. 2020 06:05:06 PM</div><div >John
<div style="color:#008040">05 nov. 2020 08:50:32 AM</div><div >Clint
etc..

(a different color style depending on the name next to "From" or "To").

I am trying to do this in Notepad . How to achieve this using regular expressions ?

Any help are welcome ! Thanks !

CodePudding user response:

Go to Search > Replace and set Search Mode option to Regular expression. The option . matches newline should be unchecked.

Then do Replace (or Replace All) two times, first we replace "From" rows then "To" rows

First for rows with

Find what: ^\s*<div >(.*.*)$

Replace with: <div style="color:#0000FF">\1

Then again for rows with

Find what: ^\s*<div >(.*.*)$

Replace with: <div style="color:#008040">\1

I added \s* at the start in case you have any identation on each row.

I do not have Notepad on this machine to test, but this regex uses only core features, so it should work. I did test it here though https://regex101.com/r/gv1XG9/1

Regex explanation:

/*
^ asserts position at start of a line
\s matches any whitespace character 
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
<div > matches the characters literally (case sensitive)

1st Capturing Group (.*.*)
. matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
 matches the characters  literally (case sensitive)
. matches any character (except for line terminators)
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)

$ asserts position at the end of a line
*/

Edit: for 2 capturing groups with the space before AM/PM captured by the 2nd capturing group

Find what: ^\s*<div >(.*)( [AP]M<\/div><div .*)$
Replace with: <div style="color:#0000FF">\1\2


Find what: ^\s*<div >(.*)( [AP]M<\/div><div .*)$
Replace with: <div style="color:#008040">\1\2

See also here: https://regex101.com/r/FAqe8K/1

For more capturing groups you can add more parenthesis ( ). Then \1, \2, \3 and so on become the captured groups contents in the "Replace with" field.

  • Related