Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正则表达式(Python),用于从<;及>;-e、 g.<;stringone>&书信电报;字符串二>;等_Python_Regex_String_Tags_Extraction - Fatal编程技术网

正则表达式(Python),用于从<;及>;-e、 g.<;stringone>&书信电报;字符串二>;等

正则表达式(Python),用于从<;及>;-e、 g.<;stringone>&书信电报;字符串二>;等,python,regex,string,tags,extraction,Python,Regex,String,Tags,Extraction,我目前正在处理堆栈溢出数据转储,并试图构造(我想象的是)一个简单的正则表达式来从字符内部提取标记名。因此,对于每个问题,我都有一个包含一个或多个标记的列表,如…,我正试图提取一个标记名列表。以下是从数据转储中提取的几个标记字符串示例: <javascript><internet-explorer> <c#><windows><best-practices><winforms><windows-services>

我目前正在处理堆栈溢出数据转储,并试图构造(我想象的是)一个简单的正则表达式来从
字符内部提取标记名。因此,对于每个问题,我都有一个包含一个或多个标记的列表,如
,我正试图提取一个标记名列表。以下是从数据转储中提取的几个标记字符串示例:

<javascript><internet-explorer>

<c#><windows><best-practices><winforms><windows-services>

<c><algorithm><sorting><word>

<java>


作为参考,我不需要将标记名划分为单词,因此对于像
这样的示例,我希望返回
最佳实践
(而不是
最佳实践
实践
)。另外,不管它值多少钱,如果有什么不同的话,我会使用Python。有什么建议吗?

由于Stackoverflow的标记名没有嵌入
,您可以使用正则表达式:

<(.*?)>

]*)>
说明:


由于Stackoverflow的标记名没有嵌入
,因此可以使用正则表达式:

<(.*?)>

]*)>
说明:


    • 这里有一个快速而肮脏的解决方案:

      #!/usr/bin/python
      
      import re
      pattern = re.compile("<(.*?)>")
      data = """
      <javascript><internet-explorer>
      
      <c#><windows><best-practices><winforms><windows-services>
      
      <c><algorithm><sorting><word>
      
      <java>
      """
      
      for each in pattern.findall(data):
          print each
      
      #/usr/bin/python
      进口稀土
      模式=重新编译(“”)
      data=”“”
      """
      对于pattern.findall中的每一个(数据):
      打印每个
      
      更新


      法定警告:如果数据转储是XML或JSON格式的(正如一位用户所评论的),那么最好使用合适的XML或JSON解析器

      这里有一个快速而肮脏的解决方案:

      #!/usr/bin/python
      
      import re
      pattern = re.compile("<(.*?)>")
      data = """
      <javascript><internet-explorer>
      
      <c#><windows><best-practices><winforms><windows-services>
      
      <c><algorithm><sorting><word>
      
      <java>
      """
      
      for each in pattern.findall(data):
          print each
      
      #/usr/bin/python
      进口稀土
      模式=重新编译(“”)
      data=”“”
      """
      对于pattern.findall中的每一个(数据):
      打印每个
      
      更新


      法定警告:如果数据转储是XML或JSON格式的(正如一位用户所评论的),那么最好使用合适的XML或JSON解析器

      您可能会对使用和json感兴趣,而不是进行数据转储(不管它们是什么)和使用regex

      例如,要从该问题中剔除标记,可以执行以下操作:

      import urllib2
      import json
      import gzip
      import cStringIO
      
      f=urllib2.urlopen('http://api.stackoverflow.com/1.0/questions/3708418?type=jsontext')
      g=gzip.GzipFile(fileobj=cStringIO.StringIO(f.read()))
      j=json.loads(g.read())
      
      print(j['questions'][0]['tags'])
      # [u'python', u'regex']
      

      您可能对使用和json感兴趣,而不是进行数据转储(不管它们是什么)和使用regex

      例如,要从该问题中剔除标记,可以执行以下操作:

      import urllib2
      import json
      import gzip
      import cStringIO
      
      f=urllib2.urlopen('http://api.stackoverflow.com/1.0/questions/3708418?type=jsontext')
      g=gzip.GzipFile(fileobj=cStringIO.StringIO(f.read()))
      j=json.loads(g.read())
      
      print(j['questions'][0]['tags'])
      # [u'python', u'regex']
      

      数据转储的格式是什么?XML?Json?数据转储的格式是什么?XML?Json?