正则表达式|记事本|搜索具有指定内容的整个XML标记并选择它

时间:2018-01-15 07:25:27

标签: regex xml

使用下面的正则表达式,我得到单个XML元素“lesson”。

(.*?)<lesson id=(.*)[\s\S]*?<\/lesson>

但是如何搜索具有特定内容的XML元素并在Notepad ++中标记整个XML元素“课程”,由此我可以将XML元素复制到另一个文件中。

使用以下内容查找并标记XML元素“课程”:

<assigned_day>5</assigned_day>

XML示例

<lesson id="LS_18300">
  <periods>2</periods>
  <lesson_subject id="SU_Gs"/>
  <lesson_teacher id="TR_Kl"/>
  <lesson_classes id="CL_MM4A"/>
  <teacher_value>200000</teacher_value>
  <effectivebegindate>20170814</effectivebegindate>
  <effectiveenddate>20180706</effectiveenddate>
  <block>2</block>
  <occurence>11111FF11111FF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF1111FFF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF11111FF11111FF1111FFFF1111FFFFFFFFFFFFFFFF11111FF1F111FF111FFFF11111FFF1111FF11111FF11111FF11111FF11111FF11111FF1111F</occurence>
  <times>
    <time>
      <assigned_day>1</assigned_day>
      <assigned_period>10</assigned_period>
      <assigned_starttime>1520</assigned_starttime>
      <assigned_endtime>1605</assigned_endtime>
      <assigned_room id="RM_E13"/>
    </time>
    <time>
      <assigned_day>1</assigned_day>
      <assigned_period>11</assigned_period>
      <assigned_starttime>1615</assigned_starttime>
      <assigned_endtime>1700</assigned_endtime>
      <assigned_room id="RM_E13"/>
    </time>
  </times>
</lesson>
<lesson id="LS_18400">
  <periods>1</periods>
  <lesson_subject id="SU_Finw"/>
  <lesson_teacher id="TR_Cp"/>
  <lesson_classes id="CL_MM4B"/>
  <teacher_value>100000</teacher_value>
  <effectivebegindate>20170814</effectivebegindate>
  <effectiveenddate>20180706</effectiveenddate>
  <occurence>11111FF11111FF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF1111FFF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF11111FF11111FF1111FFFF1111FFFFFFFFFFFFFFFF11111FF1F111FF111FFFF11111FFF1111FF11111FF11111FF11111FF11111FF11111FF1111F</occurence>
  <times>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>5</assigned_period>
      <assigned_starttime>1100</assigned_starttime>
      <assigned_endtime>1145</assigned_endtime>
      <assigned_room id="RM_113"/>
    </time>
  </times>
</lesson>
<lesson id="LS_23200">
  <periods>3</periods>
  <lesson_subject id="SU_W+G"/>
  <lesson_teacher id="TR_Mü"/>
  <lesson_classes id="CL_E3B"/>
  <teacher_value>300000</teacher_value>
  <effectivebegindate>20170814</effectivebegindate>
  <effectiveenddate>20180706</effectiveenddate>
  <block>3</block>
  <occurence>11111FF11111FF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF1111FFF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF11111FF11111FF1111FFFF1111FFFFFFFFFFFFFFFF11111FF1F111FF111FFFF11111FFF1111FF11111FF11111FF11111FF11111FF11111FF1111F</occurence>
  <times>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>3</assigned_period>
      <assigned_starttime>0910</assigned_starttime>
      <assigned_endtime>0955</assigned_endtime>
      <assigned_room id="RM_E14"/>
    </time>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>4</assigned_period>
      <assigned_starttime>1010</assigned_starttime>
      <assigned_endtime>1055</assigned_endtime>
      <assigned_room id="RM_E14"/>
    </time>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>5</assigned_period>
      <assigned_starttime>1100</assigned_starttime>
      <assigned_endtime>1145</assigned_endtime>
      <assigned_room id="RM_E14"/>
    </time>
  </times>
</lesson>
<lesson id="LS_25000">
  <periods>2</periods>
  <lesson_subject id="SU_De"/>
  <lesson_teacher id="TR_Bt"/>
  <lesson_classes id="CL_M3A"/>
  <teacher_value>200000</teacher_value>
  <effectivebegindate>20170814</effectivebegindate>
  <effectiveenddate>20180706</effectiveenddate>
  <block>2</block>
  <occurence>11111FF11111FF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF1111FFF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF11111FF11111FF1111FFFF1111FFFFFFFFFFFFFFFF11111FF1F111FF111FFFF11111FFF1111FF11111FF11111FF11111FF11111FF11111FF1111F</occurence>
  <times>
    <time>
      <assigned_day>5</assigned_day>
      <assigned_period>3</assigned_period>
      <assigned_starttime>0910</assigned_starttime>
      <assigned_endtime>0955</assigned_endtime>
      <assigned_room id="RM_226"/>
    </time>
    <time>
      <assigned_day>5</assigned_day>
      <assigned_period>4</assigned_period>
      <assigned_starttime>1010</assigned_starttime>
      <assigned_endtime>1055</assigned_endtime>
      <assigned_room id="RM_226"/>
    </time>
  </times>
</lesson>
<lesson id="LS_25200">
  <periods>2</periods>
  <lesson_subject id="SU_FRW"/>
  <lesson_teacher id="TR_Md"/>
  <lesson_classes id="CL_M3A"/>
  <teacher_value>200000</teacher_value>
  <effectivebegindate>20170814</effectivebegindate>
  <effectiveenddate>20180706</effectiveenddate>
  <block>2</block>
  <occurence>11111FF11111FF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF1111FFF11111FF11111FF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FFFFFFFFFFFFFFFF11111FF11111FF11111FF11111FF11111FF11111FF1111FFFF1111FFFFFFFFFFFFFFFF11111FF1F111FF111FFFF11111FFF1111FF11111FF11111FF11111FF11111FF11111FF1111F</occurence>
  <times>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>1</assigned_period>
      <assigned_starttime>0730</assigned_starttime>
      <assigned_endtime>0815</assigned_endtime>
      <assigned_room id="RM_114"/>
    </time>
    <time>
      <assigned_day>4</assigned_day>
      <assigned_period>2</assigned_period>
      <assigned_starttime>0820</assigned_starttime>
      <assigned_endtime>0905</assigned_endtime>
      <assigned_room id="RM_114"/>
    </time>
  </times>
</lesson>

一次尝试是这样的:

(.*?)<lesson id=(.*)[\s\S]*?(assigned_day>5)?<\/lesson>

1 个答案:

答案 0 :(得分:1)

你可以试试这个正则表达式:

<lesson\s+id[^>]*>(?=(?:(?!<\/lesson>).)*<assigned_day>5<\/assigned_day>).*?<\/lesson>

p.s:确保在记事本++编辑器中检查._matches换行符

Demo