Search code examples
notepad++notepad

I need a function that separate a word from everything else in the line


For example,

Line1 “gamertag: johnny51 you can add me here”

Line2 "gamertag: nicholas362 this is my tag add to play"

After a function I would like only to see

Line 1 “johnny51”

Line 2 "nicholas362"

Is this even possible? If it is, I could really use some help. Thanks everybody.


Solution

  • Short version:

    Find what: ^([a-zA-Z]+): ([a-zA-Z0-9_]+) (.*)$

    Replace with: \2

    You need to select Regular expression in the replace dialog and deselect the . matches new line

    Longer version:

    I guess that you want to match the text with a regular expression. There are many sources of information about the regular expressions (https://en.wikipedia.org/wiki/Regular_expression). Your text can be matched with the following pattern (for "Find what"):

    ^([a-zA-Z]+): ([a-zA-Z0-9_]+) (.*)$
    

    You read that as:

    • ^ means "begin the matching from the first character in the line";
    • ([a-zA-Z]+) means "match a group that is made of at least one lower case or upper case character" (the + sign means at least one of the previous; [ ] means one of; and a-z and A-Z are ranges of characters that are valid to be matched);
    • then this group have to be followed by : (two dots and a space);
    • after that, another group must be matched. This second group must have at least one (again the +) of lower case, upper case, digit, or underscore characters;
    • then, there is again a space , then third group made of zero or more of any character (here . means any character, and * means the element before it is matched from zero to infinity number of times);
    • after all of that is the end of the line $ (called sentinel).

    After each line is matched by the pattern, you will have 3 matched groups. You can use them into the "Replace with" edit and construct what ever you need like that:

    _\1_\2_\3
    

    If the source text is that:

    gamertag: johnny51 you can add me here
    gamertag: nicholas362 this is my tag add to play
    
    

    The output will be that:

    _gamertag_johnny51_you can add me here
    _gamertag_nicholas362_this is my tag add to play
    

    To get the output exactly as you wanted it, use only \2 for the "Replace with". This means delete the matched text and in its place put the second group. The second group is the name of the player. You will get this:

    johnny51
    nicholas362
    

    Care that the lines are exactly in this format, else the regular expression will not match them.