使用grep和正则表达式选择跨多行的文本 [英] Selecting text spanning multiple lines using grep and regular expressions

查看:91
本文介绍了使用grep和正则表达式选择跨多行的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 xs:element 标签匹配仅包含 minOccurs 的行.如下所示,其中一些在一行上包含两个搜索条件,其中一些跨多行.有没有一种使用grep和正则表达式选择它们的方法.

I'm trying to match lines with the xs:element tag that only contain minOccurs. As seen below some of them contain both search criteria on one line, some of them span multiple lines. Is there a way of selecting them using grep and regular expressions.

<xs:element name="shipto">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="address" type="xs:string"/>
      <xs:element name="city" minOccurs="1" type="xs:string"/>
      <xs:element name="country" 
               minOccurs="1" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

正确的输出应如下:

<xs:element name="city" minOccurs="1" type="xs:string"/>
<xs:element name="country" 
               minOccurs="1" type="xs:string"/>

推荐答案

假设格式正确的XML(即内部属性中没有未转义的>),则可以执行以下操作:

Assuming well-formed XML (i.e. no un-escaped > inside attributes) then you can probably do this:

<xs:element[^>]+?\sminOccurs\s*=[^>]+>

但是,由于grep匹配单独的行,因此我不确定这是否适用于grep,因此您可能需要编写一个perl脚本或执行某些操作.

However, I'm not sure this will work with grep, since grep matches individual lines, so you may need to write a perl script or something to do it.

(注意,如果您某种程度上具有包含值sminOccurs=的属性,则需要变得更聪明,但是由于这似乎是地址数据,因此我认为这不太可能,并手动删除了所有内容

(Note, if you somehow have attributes which contain the value sminOccurs= then you'd need to get cleverer, but since this appears to be address data, I'm assuming that's unlikely, and manually removing any that happen to occur isn't going to be a problem.)

这篇关于使用grep和正则表达式选择跨多行的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆