How to find and replace contents of a bracket inside notepad++

31,321

Solution 1

Here is mine exression for notepad ([0-9(). ]*)(.*)(\s\()(.*)

  1. You need split your search in groups

    1. ([0-9. ]*) numbers, spaces and dots combination in 0 or more times

    2. (.*) everything till next expression

    3. (\s\() space and opening parenthesis

    4. (.*) everything else

  2. In replace box - for practicing if you place

    1. \1\2\3\4 this do nothing :) just print all groups from above from 1.1 to 1.4

    2. \2 this way you get only 1.2 group

    3. new_thing\2new_thing adds your text before and after group

    4. <a href=blah.com/\2.html>linky study</a> so now your text is added - spaces between words can be problematic when creating link - so another expression need to be made to replace all spaces in link to i.e. _

    5. If you need add backslash as text (or other special sign used by regex) it must be escaped so you put \\ for backslash or \$ for dolar sign

Want more tune - <a href=blah.com/\2.html>\2</a> add again 1.2 group - or use whichever you want

On the screenshot you can see how I use it (I had found and replaced one line) enter image description here

Ok and then we have case 4.2 with colon at the end so simply add colon after extracted section:

change replace from \2 to \2,

Now you need join it so simplest way is to Edit->Line Operations->Join Lines but if you want to be real pro switch to Extended mode (just above Regular expression mode in Replace window) and Find \r\n and replace with space.

Removing line endings can differ in some cases but this is another story - for now I assume that you using windows since Notepad++ is windows tool and line endings are in windows style :)

Solution 2

The following regex should do the job: \d+\.\s*(.*?)\s*\(.*?\).

And the replacement: <a href=example.com\\\1.htm>\1</a>.

Explanation:

  • \d+ : Match a digit 0 or more times.
  • \. : Match a dot.
  • \s* : Match spaces 0 or more times.
  • (.*?) : Group and match everything until ( found.
  • \s* : Match spaces 0 or more times.
  • \(.*?\) : Match parenthesis and what's between it.

The replacement part is simple since \1 is referring to the matching group.

Online demo.

Solution 3

Try replacing ^\d+\.(.*) \(\w+\)$ with <a href=blah.com\\\1.htm>linky study</a>.

The ^\d+. removes the leading number and dot. The (.*) collects the words. Then there is a single space. The \(\w+\)$ matches the final number in brackets.

Update for the added Q4.

Regular expressions capture things written between round brackets ( and ). Brackets that are to be found in the text being searched must be escaped as \( and \). In the replacement expression the \1 and \2 etc are replaced by the corresponding capture expression. So a search expression such as Z(\d+)X([aeiou]+)Y might match Z29XeieiY then the replacement expression P\2Q\1R would insert PeieiQ29R. In the search at the top of this answer there is one capture, the (.) captures or collects the words and then the \1 inserts the captured words into the replacement text.

Share:
31,321
aroos
Author by

aroos

Updated on July 05, 2022

Comments

  • aroos
    aroos almost 2 years

    I have a large file with content inside every bracket. This is not at the beginning of the line.

    1. Atmos-phere (7800)
    2. Atmospheric composition (90100)
    3.Air quality (10110)
    4. Atmospheric chemistry and composition (889s120)
    5.Atmospheric particulates (10678130)
    

    I need to do the following

    1. Replace the entire content, get rid of line numbers
      1.Atmosphere (10000) to plain Atmosphere

    2. Delete the line numbers as well 1.Atmosphere (10000) to plain Atmosphere

    3. make it a hyperlink 1.Atmosphere (10000) to plain <a href="http://blahqd.com/Atmosphere.htm">linky study</a>

    4. [I added/Edit] Extract the words into a new file, where we get a simple list of key words. Can you also please explain the numbers in replace the \1\2, and escape on some characters

      1. Each set of key words is a new line
        Atmospheric
        Atmospheric composition
        Air quality

      2. Each set is a on one line separated by one space and commas
        Atmospheric, Atmospheric composition, Air quality

    I tried find with regex like so, \(*\) it finds the brackets, but dont know how to replace this, and where to put the replace, and what variable holds the replacement value.

    • AdrianHHH
      AdrianHHH almost 11 years
      It may be better to create a new question rather than adding more bits on to your original question.
  • Pshemo
    Pshemo almost 11 years
    Beside numbers OP have also characters inside brackets.
  • AdrianHHH
    AdrianHHH almost 11 years
    @Pshemo thanks for spotting that. Changed \(\d+\)$ to \(\w+\)$.
  • aroos
    aroos almost 11 years
    Thanks for the broken down explanation, I was trying to break it down and understand like you said. But when I tried in Notepad, notepad says "could not find any text matching ([0-9(). ]?)(.*)(\s()(.*)"
  • pbaranski
    pbaranski almost 11 years
    Hey man - you omitted escape in open parenthesis in (\s() so it should look (\s\() and another fix is that I replaced ? to * in first expression
  • aroos
    aroos almost 11 years
    You have the most answers.
  • aroos
    aroos almost 11 years
    You have the most answers. You solved) Scenario 2 and 4.1. Your explanation was easier to follow. However, when I put \2 in replace, it gives me funky ouput - S( on every line.
  • pbaranski
    pbaranski almost 11 years
    Try it now - I added some further explanations
  • pbaranski
    pbaranski almost 11 years