71

I have a part of HTML source file that contains strings that I want to select and copy at once, using the regex functionality of Notepad++.

Here is a part of the text source:

<option value="Performance"
>Performance</option>
<option value="Maintenance"
>Maintenance</option>
<option value="System Stability"
>System Stability</option>

I'm using the regex "[0-9a-zA-Z ]*" to search the "value" values. I have also selected the feature in Notepad++ search to highlight/mark the found text. This working fine I now want to copy or cut only the highlighted text to clipboard for further processing. But I'm not able to find this functionality in Notepad++. Is this simply not possible or am I too dumb?

Alex
  • 723
  • 1
  • 5
  • 4
  • I'm not familiar with notepad++, but doesn't Ctrl-C work (as it does everywhere else in Windows), or are you looking for a scriptable way of doing it, or what? – James Curran Feb 19 '10 at 19:25
  • Ctrl-C does not copy the strings. It looks like Notepad++ has to different ways to mark and to highlight text in the buffer. The text matched by the regex is highlighted in some color, the text marked with the mouse for example is highlighted in grey. This text can be copied with Ctrl-C. But you cannot mark different parts of the text at once. – Alex Feb 19 '10 at 19:32
  • [Ankit's answer](http://superuser.com/questions/477628/export-all-regular-expression-matches-in-textpad-or-notepad-as-a-list) over at superuser is a way to do it using just Notepad++ – Fidel Oct 20 '16 at 18:08

10 Answers10

105

As of Notepad++ 5.9 they added a feature to 'Remove Unmarked Lines' which can be used to strip away everything that you don't want along with some search and replaces for the other text on each value line.

  1. Use the Search-->Find-->Mark functionality to mark each line you want to keep/copy and remember to tick 'Bookmark Line' before marking the text
  2. Select Search-->Bookmark-->Remove Unmarked Lines
  3. Use Search-->Find-->Replace to replace other text you do not want to keep/copy with nothing
  4. Save the remaining text or copy it.

You can also do a similar thing using Search-->Bookmark-->Copy Bookmarked Lines

So technically you still cannot copy marked text, but you can bookmark lines with marked text and then perform various operations on bookmarked or unmarked lines.

Colin
  • 1,229
  • 2
  • 9
  • 5
  • +1 - Great little tip and very handy when trawling (large) log files for specific entries. – CraigTP Jul 30 '12 at 12:17
  • thanks. i followed this guide to extract marked lines. Then read a instruction at https://notepad-plus-plus.org/community/topic/10839/copy-regular-expression-search-results-to-clipboard/5 link to extract only matched text. SEARCH .*?(\d+ +\w+).* REPLACE \1 – Nguyễn Văn Vinh May 20 '16 at 07:33
26

I am adding this for completeness as this post hits high in Google search results.

You can actually copy all from a regex search, just not in one step.

  1. Use Mark under Search and enter the regex in Find What.
  2. Select Bookmark Line and click Mark All.
  3. Click Search -> Bookmark -> Copy Bookmarked Lines.
  4. Paste into a new document.
  5. You may need to remove some unwanted text in the line that was not part of the regex with a search and replace.
gschoep
  • 473
  • 5
  • 6
  • 2
    Nr 5 in this list is the time consuming part for me. Since copy marked text is obviously not supported by NotePad++, I resorted to another piece of free software: Expresso by Ultrapico. I pasted the whole text into the "Sample text" pane of Expresso, and the same regexp search that I had prepared in Notepad++ into the "Regular expressions" pane. I then pressed "Run match", right clicked in the "Search results pane" / "Copy matched text to clipboard". – Andreas Jansson Nov 19 '15 at 10:40
20

Try this instead:

First, fix the line ending problem: (Notepad++ doesn't allow multi-line regular expressions)

Search [Extended Mode]: \r\n> (Or your own system's line endings)

Replace: >

then

Search [Regex Mode]: <option[^>]+value="([^"]+)"[^>]*>.*

(if you want all occurences of value rather than just the options, simple remove the leading option)

Replace: \1

Explanation of the second regular expression:

<option[^>]+     Find a < followed by "option" followed by 
                 at least one character which is not a >

value="          Find the string value="

([^"]+)          Find one or more characters which are not a " and save them
                 to group \1

"[^>]*>.*        Find a " followed by zero or more non-'>' characters
                 followed by a > followed by zero or more characters.

Yes, it's parsing HTML with a regex -- these warnings apply -- check the output carefully.

Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • +1 was about to post something similar to this. Good answer. – Beanish Feb 19 '10 at 19:44
  • This might be the right direction. But group \1 only contains the first value, here "Performance". the rest is not stored in group. – Alex Feb 19 '10 at 20:03
  • Alex, are you sure ... I tried this out in Notepad++ using your data and got each of the values on its own line. – Sean Vieira Feb 19 '10 at 20:06
  • You're right. It works. I stripped away all line feeds in step one. Thus matching text in only one line, it gave only one result. Thanks alot. – Alex Feb 19 '10 at 21:29
20

This is similar to https://superuser.com/questions/477628/export-all-regular-expression-matches-in-textpad-or-notepad-as-a-list.

I hope you are trying to extract :
"Performance"
"Maintenance"
"System Stability"

Here is the way - Step 1/3: Open Search->Find->Replace Tab , select Regular Expression Radio button. Enter in Find what : (\"[a-zA-Z0-9\s]+\") and in Replace with : \n\1 and click Replace All buttton. Before Clicking Replace All

Step 2/3: After first step your keywords will be in next lines.(as shown in next image). Now go to Mark tab and enter the same regex expression in Find what: Field. Put check mark on Bookmark Line. Then Click Mark All. Bookmark the lines

Step 3/3 : Goto Search -> Bookmarks -> Remove unmarked lines.Remove Unmarked lines

So you have the final result as belowFinal Result

Chetan
  • 644
  • 6
  • 7
  • This doesn't work with multi-line regex. Only the first line gets "bookmarked" while the lines after are only "marked". How do you keep the "marked" lines too? – Al G Johnston Mar 23 '20 at 20:20
12

It would be a great feature to have in Notepad++. I use the following technique to extract all the matches out of a file:

powershell
select-string -Path input.txt -Pattern "[0-9a-zA-Z ]*" -AllMatches | % { $_.Matches } | select-object Value > output.txt

And if you'd like only the distinct matches in a sorted list:

powershell
select-string -Path input.txt -Pattern "[0-9a-zA-Z ]" -AllMatches | % { $_.Matches } | select-object Value -unique | sort-object Value > output.txt
Fidel
  • 7,027
  • 11
  • 57
  • 81
  • 1
    wholly crap, ur a life saver – dbinott Jan 27 '17 at 22:10
  • 1
    Amazing! Much better than the Notepad++ solutions. – blindstuff Feb 23 '17 at 18:52
  • How can we use this? Where should we put this code? Thanks if you can explain a bit more.... – Happy Bird Mar 23 '18 at 21:03
  • how could i make this work for searching for a string and selecting that line plus 2 lines above that, finding all those 3 line sets, and exporting them? currently using (?m)(^[^\r\n]*\R+){2}STRING[^\r\n]*\R+(^[^\r\n]*\R+){0} in notepad++ but i have to go find, cut, paste, repeat. – droopie Dec 08 '18 at 17:22
12

"Copy Marked Text" is now a Built-in function in Notepad++ – Illustrated answer

My installed version of Notepad++ was a few years old, and I had turned off auto updates. After updating Notepad++ to the latest version (8.1.1), I found out that copying marked text is indeed a supported function nowadays!

I take the liberty of making this an answer of it's own, even though there are comments above trying to explain the same thing. In this way I'm able to illustrate with a picture:

enter image description here

A lengthier way for the same result would be to "Mark all", and go to the menu "Search". Then select "Copy styled text / Find style (marked)".

Andreas Jansson
  • 830
  • 1
  • 9
  • 21
4

In Notepad++ v7.9.3 they provide option to copy marked text. Go to Search menu and select mark option..(Ctrl+M) Enter what you want to find and click mark all option then click on copy marked text and paste wherever you want.

You can copy multiple mark lines using regular expression option in search mode.

A simple example : let’s suppose that you are looking for the litteral string 12345, anywhere, on a line.

First, to match all the contents of that specific line, as well as its End of Line characters, just use the regex : ^.*12345.*\R

Secondly, to select : All the contents of that line and the next 10 lines, use the regex : ^.*12345.*\R(.*\R){10}

All the contents of that line and the previous 10 lines, use the regex : (.*\R){10}^.*12345.*\R

All the contents of that line and the 5 lines, before and after this line, use the regex : (.*\R){5}^.*12345.*\R(.*\R){5}

Note : For the third example, another syntax, that uses a subroutine call, to the group 1, (?1), is possible : (.*\R){5}^.*12345.*\R(?1){5}

Toto
  • 89,455
  • 62
  • 89
  • 125
Pyk Patel
  • 41
  • 3
  • If only it were as simple as **"click mark all option then click on copy marked text and paste wherever you want."** I see no "Copy marked text" option anywhere. Please explain how. – LilGames May 18 '21 at 19:55
  • 1
    Hello @LilGames Please update Notepad++ to Notepad++ **v7.9.3 version**. In this version you will find **Copy marked text** option. – Pyk Patel May 19 '21 at 05:12
2

No, as of Notepad++ 5.6.2, this doesn't seem to be possible. Although column selection (Alt+Selection) is possible, multiple selections are obviously not implemented and thus also not supported by the search function.

AndiDog
  • 68,631
  • 21
  • 159
  • 205
  • The strange thing is that 5.6.6 does allow to highlight the matching text parts in some color, it only does not allow to put it them to clipboard. – Alex Feb 19 '10 at 19:41
0

I had the same problem. You can list the regex matches in a new tab, every match in new line in PSPad Editor, which is very similar as Notepad++.

Hit Ctrl + F to search, check the regexp opion, put the regexp and click on List.

dodo
  • 459
  • 2
  • 8
  • 21
0

It's not possible with Notepad but HERE'S THE EASY SOLUTION:

You will need the freeware Expresso v3.1 http://www.ultrapico.com/ExpressoDownload.htm

I resorted to another piece of free software: Expresso by Ultrapico.

  1. Once installed go in the tab "Test Mode".
  2. Copy your REGEX into the "Regular expressions" pane.
  3. Paste your whole text to be searched into the "Sample text" pane of Expresso,

  4. Press the "Run match" button. Right click in the "Search results pane" and "Export to..." or "Copy matched text to clipboard".

N.B.: The original author is @Andreas Jansson but it is hidden in a comment, so since this page is high ranked in Google Search I leave it here for others.

Khado Mikhal
  • 602
  • 7
  • 14
  • This answer does not apply anly longer. Expresso is no longer needed. Today I found out that copying marked text is supported in later versions of Notepad++. See my newest answer in this thread. – Andreas Jansson Jul 07 '21 at 06:34