如何替换以特定字符结尾的所有单词?

时间:2016-11-05 10:23:53

标签: python regex

我有像这样的HTML

<b>Source:</b> <a href=\'http://archive.ics.uci.edu/ml/datasets/Iris\'>UCI Machine Learning Repository</a><br>Creator: <br>R.A. Fisher<br>Donor: <br>Michael Marshall (MARSHALL%<u>PLU <b>\'@\'</b> io.arc.nasa.gov</u>)<br><b>Abstract:</b>  Famous database; from Fisher, 1936<br><b>Data Set Information:</b> This is perhaps the best known database to be found in the pattern recognition literature.  Fisher\'s paper is a classic in the field and is referenced frequently to this day.  (See Duda &amp; Hart, for example.)  The data set contains 3 classes of 50 instances each, where each class refers to a type of iris plant.  One class is linearly separable from the other 2; the latter are NOT linearly separable from each other.<br>Predicted attribute: class of iris plant.<br>This is an exceedingly simple domain.<br>This data differs from the data presented in Fishers article (identified by Steve Chadwick,  <u>spchadwick <b>\'@\'</b> espeedaz.net</u> ).  The 35th sample should be: 4.9,3.1,1.5,0.2,"Iris-setosa" where the error is in the fourth feature. The 38th sample: 4.9,3.6,1.4,0.1,"Iris-setosa" where the errors are in the second and third features.  <br><b>Attribute Information:</b><br>   1. sepal length in cm<br>   2. sepal width in cm<br>   3. petal length in cm<br>   4. petal width in cm<br>   5. class: <br>      -- Iris Setosa<br>      -- Iris Versicolour<br>      -- Iris Virginica

我想在<b></b>标记内包装所有以:(冒号)结尾的单词

Python中的Regex是做什么的?

我试过这个正则表达式\b(\w+:)\b但它不起作用。

3 个答案:

答案 0 :(得分:1)

试试这个RegEx:

<b>[A-Za-z ]{1,}\:</b>

将所有这些单词提取到列表中,然后执行您想要执行的任何处理。

答案 1 :(得分:1)

这是您需要的正则表达式<b>\w+:<\/b>

Try it yourself

答案 2 :(得分:0)

import re

regex = "<b>bold string with colon:</b>"

matchObj = re.match(r'<b>(.*):</b>', regex, 0)
if matchObj:
    print matchObj.group()