6

すべての括弧内にコンテンツを含む大きなファイルがあります。これは行頭ではありません。

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

私は次のことをする必要があります

  1. コンテンツ全体を置き換え、行番号
    1.Atmosphere (10000)を削除してプレーンにしますAtmosphere

  2. 行番号も 1.Atmosphere (10000)プレーンに削除Atmosphere

  3. 1.Atmosphere (10000)プレーンへのハイパーリンク にする<a href="http://blahqd.com/Atmosphere.htm">linky study</a>

  4. [追加/編集]単語を新しいファイルに抽出し、キーワードの簡単なリストを取得します。また、\1\2 を置き換える数字について説明し、一部の文字をエスケープすることもできますか

    1. キーワードの各セットは新しい行です
      Atmospheric
      Atmospheric composition
      Air quality

    2. 各セットは、1 つのスペースとコンマで区切られた 1 行にあります。
      Atmospheric, Atmospheric composition, Air quality

私はそのように正規表現で検索しようとし\(*\)ましたが、括弧を見つけましたが、これを置き換える方法、置き換えをどこに置くべきか、どの変数が置き換え値を保持するのかわかりません。

4

3 に答える 3

8

次の正規表現がその仕事をするはずです: \d+\.\s*(.*?)\s*\(.*?\).

そして置換: <a href=example.com\\\1.htm>\1</a>.

説明:

  • \d+: 数字に 0 回以上一致します。
  • \.: ドットに一致します。
  • \s*: 0 回以上のスペースに一致します。
  • (.*?): 見つかるまですべてをグループ化して一致させ(ます。
  • \s*: 0 回以上のスペースに一致します。
  • \(.*?\): 括弧とその間にあるものを一致させます。

\1一致するグループを参照しているため、置換部分は簡単です。

オンラインデモ

于 2013-05-26T16:48:22.590 に答える
2

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.

于 2013-05-26T16:24:58.523 に答える