zoukankan      html  css  js  c++  java
  • Regex

    We need to add a prefix "prefix-" to <MsgId><Id> element in xml messages but there might be a new line between <MsgId> and <Id>.

    Example of the xml messages are

            <MsgId>
              <Id>XXX</Id>
              <CreDtTm>2018-03-22T09:05:24.334054Z</CreDtTm>
            </MsgId>
            <Els>
                  <Id>BBB</Id>
            </Els>
    
    
    
            <MsgId><Id>XXX</Id>
              <CreDtTm>2018-03-22T09:05:24.334054Z</CreDtTm>
            </MsgId>
            <Els> <Id>BBB</Id></Els>

    1. Greedy match

     file_content = re.sub('(<MsgId>.*(\n)?.*<Id>)', r'1' + PARALLEL_PREFIX, file_content)

    regular expression will match the element using greedy method by default, so the above regex will actually match "<MsgId>...</MsgId>..<Els><Id>" not "<MsgId><Id>"

    2. Lazy match

    Adding "?" to quantifiers like "+", "?", "*" will do a lazy match instead of greedy match. so

     file_content = re.sub('(<MsgId>.*?(\n)??.*?<Id>)', r'1' + PARALLEL_PREFIX, file_content)

    the above regex will match "<MsgId><Id>", generally means match the keyword using as less characters as possible.

  • 相关阅读:
    TypeScript学习笔记
    Spring基础知识
    Filter基础知识
    如何开发自定义标签
    会话和会话状态
    Servlet转发到JSP页面的路径问题
    JDBC相关知识
    gimp 很强大, 可是不会用
    python 启动文件
    minidnla policy
  • 原文地址:https://www.cnblogs.com/codingforum/p/8711823.html
Copyright © 2011-2022 走看看