2015-06-26 28 views
0

無序列表,我有以下字符串(請注意有每個連字符之前新的空間):形式的正則表達式

string = ' - Bullet 1\ 
    - Bullet 2\ 
    - Bullet 3\ 
    - Bullet 4' 

我想使用Python和正則表達式,以便它是一個HTML列表替換像這樣:

<ul> 
    <li>Bullet 1</li> 
    <li>Bullet 2</li> 
    <li>Bullet 3</li> 
    <li>Bullet 4</li> 
</ul> 

正如你可以看到,我想用<li></li>代碼來替換每一顆子彈,然後用<ul></ul>標籤包裝他們。

該字符串不會總是保持不變,所以我寧願如果有一種方法來識別第一個項目符號列表項目以放置第一個<ul>,然後用一種方法來識別最後一個項目符號列表項目,然後包括</ul>

事情我已經試過了,沒有工作:

  • re.sub(r'(\ \ -\ (.*?))', r'<li>\1</li>', string)
  • 我讀了添加了許多以應用re.sub像re.sub(r'', r'', string, 1)將適用東西的出現(在這種情況下,第一一個,因爲它是1)。

編輯:

如果語法與任何字符串,如工作,我寧願:

string = 'This is some text - Bullet 1\ 
    - Bullet 2\ 
    - Bullet 3\ 
    - Bullet 4' 

正如你所看到的,在頂部的一些文字是不子彈形式。這個文本可能會有所不同,或者可能在項目符號列表等之後,所以語法將不得不與此一起工作。

+0

字符串列表後面有什麼?那麼,它是「blah {list} blah」還是隻是「blah {list}」 – dantiston

+0

是的,這就是在列表之前和/或之後可能有某種東西。 –

+0

@PavSidhu在你的字符串中的子彈在每一個新行或'\'只是爲了延續? –

回答

2

假設子彈項目是每行一個這樣的:

>>> string = '''This is some normal text 
    - Bullet 1 
    - Bullet 2 
    - Bullet 3 
    - Bullet 4 
This is other text at the end''' 

還沒有周圍的文本另一個符號列表:

>>> string2 = ''' - Bullet 1 
    - Bullet 2 
    - Bullet 3 
    - Bullet 4''' 

有兩個簡單的替換y歐也可以代替子彈在正常文本中間:

def htmlize(txt): 
    return re.sub(' - ([^\n]*)', r'<li>\1</li>', 
     re.sub('(( - [^\n]*(\n|$))+)', r'<ul>\n\1\n</ul>', txt)) 

測試:

>>> htmlize(string) 
'This is some normal text\n<ul>\n<li>Bullet 1</li>\n<li>Bullet 2</li>\n<li>Bullet 3</li>\n 
<li>Bullet 4</li>\n\n</ul>This is other text at the end' 
>>> htmlize(string2) 
'<ul>\n<li>Bullet 1</li>\n<li>Bullet 2</li>\n<li>Bullet 3</li>\n<li>Bullet 4</li>\n</ul>' 

編輯:

>>> string3 = 'This is some text to introduce the bullet points:\n - This is the first bullet points\n - This is the second bullet point\n - This is the third bullet point\nThis some last bit of text.' 
>>> htmlize(string3) 
'This is some text to introduce the bullet points:\n<ul>\n<li>This is the first bullet points</li>\n<li>This is the second bullet point</li>\n<li>This is the third bullet point</li>\n\n</ul>This some last bit of text.' 
+0

啊,這是一個錯誤,只是修正了它。我認爲我們現在應該刪除這些評論以刪除不需要的評論。 –

0
string = ''' - Bullet 1 
    - Bullet 2 
    - Bullet 3 
    - Bullet 4''' 

newstring = [line[4:].join(['<li>', '</li>']) if 
      line.startswith(' - ') else line for line in string.split('\n')] 

結果:

>>> print(*newstring, sep='\n') 
<li>Bullet 1</li> 
<li>Bullet 2</li> 
<li>Bullet 3</li> 
<li>Bullet 4</li> 
+0

爲什麼downvote?似乎對我有用。 – TigerhawkT3

+0

我認爲它是[meta-effect](http://meta.stackoverflow.com/questions/297859/can-stack-overflow-and-metas-logos-be-changed-temporarily-to-the-loveoverflow/297878 #comment212534_297900)你提到過。 1+來對付無知。 (順便說一下,優秀的,經過深思熟慮的[回答](http://meta.stackoverflow.com/a/297900/2680216)。) –

0

如何首先提取每一個項目,你從每個Bullet想(例如),然後加入返回列表到您需要的形式,這樣:

s = 'This is some text - Bullet 1\ 
    - Bullet 2\ 
    - Bullet 3\ 
    - Bullet 4 And some text here' 

r = re.findall(r'\w+\s+\d+', s) #Get all items from your `string` 
in_Block = [' '+t.join(['<li>','</li>']) for t in r] #join your items to construct inside block 
out_Block = '\n'.join(in_Block).join(['<li>\n','\n</li>']) #Form the Outer Block 
>>> print out_Block 
<li> 
    <li>Bullet 1</li> 
    <li>Bullet 2</li> 
    <li>Bullet 2</li> 
    <li>Bullet 4</li> 
</li> 
>>> 
相關問題