notepad++ - How to use a regex to organize song lists, changing their order from "Song - Artist" to "Artist - Song"?

08
2014-07
  • screener

    I have a long list of songs in the format "Artist - Song", but I want them to be listed as "Artist - Song". How to change all items automatically with a regular expression, preferably on Notepad++?

  • Answers
  • screener

    On Notepad++:

    1. Click before the first letter in your file for the search to run throughout the complete list
    2. Press CTRL + H to open the text editor Replace dialog
    3. Check the Regular expression search mode
    4. Type (.*?)\s-\s(.*) in the "Find what" field. It means "select and save everything until a hyphen preceded by a space" and then "everything after the hyphen followed by a space"
    5. Type \2 - \1 in the Replace with field to insert the two sets reversed
    6. Click on Replace all

    Note: if it doesn't work, make sure that the list uses the same hyphen character as the one you typed into the Find what field.

    Screenshot of the Notepad++ "Replace" dialog


  • Related Question

    regex - NotePad++ - Why does finding ^ not work?
  • Jack Kada

    I am trying to move away from TextPad, and I just can't get regular expressions like ^ and $ to be replaced. I have definitely ticked the regular expression box.

    What am I doing wrong?

    I am trying to find the start of a new line. In TextPad, it is find '^' and ensure regular expressions is enabled. With Notepad++ it does not do that. It just says "Not found".


  • Related Answers
  • John Weldon

    ^ and $ are both anchors in Regex, which means if you want to replace the literal chars ^ and $ you need to escape them, usually with a leading backslash (\^, and \$).

    To find the first character on a line use ^.

    The start line anchor (^) is a zero-width match, so combining it with the . will find any character at the beginning of a line.

    Maybe you can explain what you're actually trying to do?

  • Peter Mortensen

    Because these are special characters that represent the front (^) and end of line ($). Try escaping them with a \\.

    Examples:

    Match "Cat" at the beginning of the line:

    ^Cat
    

    Match "Cat" at the end of line:

    Cat$
    

    Match "Cat" as only thing on a line:

    ^Cat$
    

    Match a "$100" within a line:

    \$100 
    

    Here is a link for the specifics on regular expression matching within Notepad++.

  • Willy

    I've had the same issue myself. After some trial and error you can achieve the same by doing the following:

    Find: ^(.)

    Replace: [the string you wish you insert]\1

    What this will do is locate and tag the first character of the line, put in the new string and put the tagged character after it.

    For example, I needed to insert a single quote at the start of each line:

    Find: ^(.)

    Replace: '\1

    You can do the same for the end of the line by doing:

    Find: (.)$

    Replace: \1*[the string you wish to append]*

  • ezequias

    I got it.

    Before

    • [email protected]
    • [email protected]
    • [email protected]
    • [email protected]

    After

    • Albert
    • Lucas
    • Rober_Klein
    • Fisher

    Remove after character or text

    @(.*)$

    Remove before character or text

    ^(.*)@

    dot = any character
    asterisk = zero or more times
    
  • Tom Wijsman

    Here is how to do it...

    Before:

    $_GET['id']; $_GET['nick'];
    

    After:

    htmlentities($_GET['id']); htmlentities($_GET['nick']);
    

    So. On the find field put:

    $_GET(.*])
    

    On the replace field put:

    htmlentities($_GET\1)