2014-11-16 59 views
-2

我在尋找以下情況的解決方案:搜索和這種情況下更換模式

原文:

= This is the first line = 
= This is the first line again 
== This is the second line == 
=== This is the third line === 
==== This is the forth line ==== 
==== The tailing '='s are optional, but if they're present, should be removed 

預期的結果:

h1. This is the first line 
h1. This is the first line again 
h2. This is the second line 
h3. This is the third line 
h4. This is the forth line 
h4. The tailing '='s are optional, but if they're present, should be removed 

任何語言好吧(python,perl,bash更好)。

+1

你只需要降價解析器 – hjpotter92

+0

你是什麼意思 '降價分析器'?其實我試圖找到工具來翻譯這種格式的文本(類似於維基)到降價,但沒有找到一個好的工具,現在我只是想在我自己的腳本中將這些行轉換爲降價格式,並且不知道它目前爲止。 – vts

回答

0

通過Python。

import re 
with open('input.txt', 'r') as f:         # Open the text file for reading 
    for line in f:             # iterate through all the lines 
     if line.startswith('='):         # Do the below operation only on the lines which starts with `=` symbol. 
      finalcount = line.split()[0].count('=')     # Count the number of `=` symbols located on the first word only. 
      line = re.sub(r'=+$', r'', line)      # Remove all the trailing `=` symbols if it's presented. 
      line = re.sub(r'^\S+', r'h'+str(finalcount)+'.', line) # Replace the first word in the lines which startswith `=` with `h` plus the value stores in finalcount variable plus a dot. 
      print(line, end='')          # Now print the modified line. 

輸出:

h1. This is the first line 
h1. This is the first line again 
h2. This is the second line 
h3. This is the third line 
h4. This is the forth line 
h4. The tailing '='s are optional, but if they're present, should be removed