Search code examples
regexvisual-studionotepad++

Copy Text that matches a regular expression


I have a regular expression that has several matches in a textfile. I want to copy only the the matches to a second file. I dont want to copy the lines that contain the matches: I only want the matched text.

I dont find a way to do this in notepad++ (only copies complete lines, not only the matches). Also not in Visual Studio search.

Is there a way to copy only matches? Maybe in grepp or sed?


Solution

  • You can do it with both. Lets say I have a following file -

    Sample file:

    [jaypal:~/Temp] cat myfile 
    this is some random number 424-555
    and my cell is 111-222-3333
    and 42555 is my zip code
    

    And I want to capture only numbers from myfile

    Using sed:

    With sed you can use the combination of -n and p option along with grouped pattern.

    sed -n 's/.[^0-9]*\([0-9-]\+\).*/\1/p'
       |   |          |          |  |  ||
        ---            ----------    -- |
         |                  |        |  ---------> `p` prints only matched output. Since
         V                  V        V              we are suppressing everything with -n
     Suppress       Escaped `(`      \1 prints      we use p to invoke printing.
     output        start the group   first matched   
                   you can reference  group
                   it with \1. If you
                   have more grouped
                   pattern then they can
                   be called with \2 ...
    

    Test:

    [jaypal:~/Temp] sed -n 's/.[^0-9]*\([0-9-]\+\).*/\1/p' myfile 
    424-555
    111-222-3333
    42555
    

    You can simply re-direct this to another file.

    Using grep:

    You can use either -

    egrep -o "regex" filename
    

    or

    grep -E -o "regex" filename
    

    From the man page:

    -E, --extended-regexp
        Interpret PATTERN as an extended regular expression (see below).
    
    -o, --only-matching
        Show only the part of a matching line that matches PATTERN.
    

    Test:

    [jaypal:~/Temp] egrep -o "[0-9-]+" myfile
    424-555
    111-222-3333
    42555
    

    You can simply re-direct this to another file.

    Note: Obviously these are simple examples but it conveys the point.