Search code examples
notepad++

How do I use a regular expression to "Find and Replace" in Notepad++


I have a text file which has 6000+ text entries like this:

<"+88016000000" time="Dec 5, 2018 2:36:03 PM" date="1543998963051" type="1" body="I am Noushin,studying  BBA final year." read="1" service_center="+8801801000016" name="" />

I need to keep the phone number and body attributes like the following:

+88016000000 I am Noushin,studying  BBA final year.

How would I write a regular expression to use in the "Find and Replace" feature of Notepad++?


Solution

  • You can use named capture groups to get the values of the attributes you want to keep. Here's a pattern that works on the example you provided. Pattern proof.

    Find what:

    <"(?<phone>\+\d+)".*?body="(?<body>[^"]*)".*?>
    

    The phone number is now available via the phone capture group, and the text via the body capture group. Reference these groups to keep the values you want.

    Replace with:

    $+{phone} $+{body}
    

    Original text

    Find and replace box

    Result

    Edit

    If you're looking for an attribute called address instead of an anonymous attribute at the beginning of the tag, you can use the following:

    <.*?address="(?<phone>\+\d+)".*?body="(?<body>[^"]*)".*?>