How to find and replace the contents of brackets inside notepad ++

I have a large file with contents inside each 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

  • Replace all content, get rid of line numbers
    1.Atmosphere (10000) to simple Atmosphere

  • Delete 1.Atmosphere (10000) line 1.Atmosphere (10000) to a simple Atmosphere

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

  • [I added / Edit] Extract the words into a new file, where we get a simple list of keywords. Can you also explain the numbers replacing \ 1 \ 2 and go out for some characters

    • Each set of keywords is a new line.
      Atmospheric
      Atmospheric composition
      Air quality

    • Each set is one line separated by one space, and commas are Atmospheric, Atmospheric composition, Air quality

I tried to find using the regular expression, so that \(*\) it finds the brackets, but does not know how to replace it, and where to put the replacement, and which variable contains the replacement value.

+6
source share
3 answers

Here is my excess for notepad ([0-9(). ]*)(.*)(\s\()(.*)

  • You need to split your search into groups.

    • ([0-9. ]*) combination of numbers, spaces and periods, 0 or more times

    • (.*) all until the next expression

    • (\s\() space and opening bracket

    • (.*) everything else

  • In the replacement field - for practice, if you post

    1. \1\2\3\4 do nothing :) just print all the groups on top from 1.1 to 1.4

    2. \2 so you get only 1.2 group

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

    4. <a href=blah.com/\2.html>linky study</a> , so now your text has been added - spaces between words can be problematic when creating a link - so you need to replace another space in the link with _

    5. If you need to add a backslash as text (or another special character used by the regular expression), it must be escaped, so you put \\ for backslash or \$ for dolar

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

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

Ok, and then we have case 4.2 with a colon at the end, so just add a colon after the highlighted section:

replace the replacement for \2 with \2,

Now you need to join it, so the easiest way is Edit-> Linear Operations-> Merge Lines but if you want to be real pro switch to Extended mode (just above Regular expression in the Replace window) and find \r\n and replace it a space.

In some cases, deleting lines may differ, but this is another story. At the moment, I assume that you are using windows, since Notepad ++ is a Windows tool, and line endings are in Windows style :)

+11
source

The following regex should complete the task: \d+\.\s*(.*?)\s*\(.*?\) .

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

Explanation:

  • \d+ : match a digit 0 or more times.
  • \. : match point.
  • \s* : match spaces 0 or more times.
  • (.*?) : group and match everything until it is found ( .
  • \s* : match spaces 0 or more times.
  • \(.*?\) : match parentheses and what's in between.

The spare part is simple, since \1 belongs to the matching group.

online demo .

+6
source

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

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

Update for added Q4.

Regular expressions capture things enclosed between parentheses ( and ) . The brackets that must be found in the text to be examined must be escaped as \( and \) . In the replacement expression \1 and \2 , etc. Replaced with the corresponding capture expression. Thus, a search expression such as Z(\d+)X([aeiou]+)Y can match Z29XeieiY , then the substitute expression P\2Q\1R will insert PeieiQ29R . When searching at the top of this answer, there is one capture, (.) Captures or collects words, and then \1 inserts the captured words into the replacement text.

+2
source

Source: https://habr.com/ru/post/945878/


All Articles