Search code examples
regexregexp-replace

Regex split using a character and replace the first part with a different string


I have list of strings as follows:

Austria: ac.at
Bangladesh: ac.bd
Belgium: ac.be
China: ac.cn
Cyprus: ac.cy
India: ac.in

And I need to get a list as follows:

$Resolved URL$ LIKE "*ac.at/*"
$Resolved URL$ LIKE "*ac.bd/*"
$Resolved URL$ LIKE "*ac.be/*"
$Resolved URL$ LIKE "*ac.cn/*"
$Resolved URL$ LIKE "*ac.cy/*"
$Resolved URL$ LIKE "*ac.id/*"

What is the best way to do this using regex replace?


Solution

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

    Find:    ^.*?(\S+)$
    Replace: $Resolved URL$ LIKE "*$1/*"
    

    Demo

    Here is an explanation of the regex:

    ^          from the start of the string
        .*?    consume (but do not capture) all content
        (\S+)  up until the final collection of non word characters and capture them
    $          end of the input