如何从python类对象获取价值

时间:2018-12-30 20:40:37

标签: python regex python-3.x

这是我的正则表达式/ python正则表达式的测试代码。

import re

htmltxt='''
<div>
    <div> name
        <div> samadhi </div>  
    </div>

    <div> age
        <div>28 </div>
    </div>

</div>
'''
opn=re.compile(r'<div')
matches01=opn.finditer(htmltxt)

for m in matches01:
    print(m)

结果

<_sre.SRE_Match object; span=(1, 5), match='<div'>
<_sre.SRE_Match object; span=(12, 16), match='<div'>
<_sre.SRE_Match object; span=(31, 35), match='<div'>
<_sre.SRE_Match object; span=(74, 78), match='<div'>
<_sre.SRE_Match object; span=(92, 96), match='<div'>

如何从结果对象访问“匹配”变量值?

1 个答案:

答案 0 :(得分:2)

/**
 * Sets the date field parameters to the values given by {@code year},
 * {@code month}, and {@code dayOfMonth}. This method is equivalent to
 * a call to:
 * <pre>
 *   setFields(Calendar.YEAR, year,
 *             Calendar.MONTH, month,
 *             Calendar.DAY_OF_MONTH, dayOfMonth);</pre>
 *
 * @param year       the {@link Calendar#YEAR YEAR} value
 * @param month      the {@link Calendar#MONTH MONTH} value
 *                   (the month numbering is <em>0-based</em>).
 * @param dayOfMonth the {@link Calendar#DAY_OF_MONTH DAY_OF_MONTH} value
 * @return this {@code Calendar.Builder}
 */
public Builder setDate(int year, int month, int dayOfMonth) {
    return setFields(YEAR, year, MONTH, month, DAY_OF_MONTH, dayOfMonth);
}

检出for m in matches01: print(m.group(1)) 模块的文档,该文档说明re返回的MathObject类:https://docs.python.org/2/library/re.html#re.MatchObject