热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Python-如何使用非字母字符拆分字符串-Python-Howtosplitastringbynonalphacharacters

Imtryingtousepythontoparselinesofc++sourcecode.TheonlythingIaminterestedinisinc

I'm trying to use python to parse lines of c++ source code. The only thing I am interested in is include directives.

我正在尝试使用python来解析c ++源代码行。我唯一感兴趣的是包含指令。

    #include "header.hpp"

I want it to be flexible and still work with poor coding styles like:

我希望它具有灵活性,仍然适用于不良的编码风格,如:

          #   include"header.hpp"  

I have gotten to the point where I can read lines and trim whitespace before and after the #. However I still need to find out what directive it is by reading the string until a non-alpha character is encountered regardless of weather it is a space, quote, tab or angled bracket.

我已经到了能够在#之前和之后读取线条和修剪空白的地步。但是我仍然需要通过读取字符串来找出它是什么指令,直到遇到非字母字符,无论天气如何,它都是空格,引号,制表符或有角度的括号。

So basically my question is: How can I split a string starting with alphas until a non alpha is encountered?

所以基本上我的问题是:我如何分割以alpha开头的字符串,直到遇到非alpha?

I think I might be able to do this with regex, but I have not found anything in the documentation that looks like what I want.

我想我可以用正则表达式做到这一点,但我没有在文档中找到任何看起来像我想要的东西。

Also if anyone has advice on how I would get the file name inside the quotes or angled brackets that would be a plus.

此外,如果有人有关于我如何获得引号或斜角括号内的文件名的建议,这将是一个加号。

7 个解决方案

#1


4  

You can do that with a regex. However, you can also use a simple while loop.

你可以用正则表达式做到这一点。但是,您也可以使用简单的while循环。

def splitnonalpha(s):
   pos = 1
   while pos 

Test:

>>> splitnonalpha('#include"blah.hpp"')
('#include', '"blah.hpp"')

#2


11  

Your instinct on using regex is correct.

你对使用正则表达式的直觉是正确的。

import re
re.split('[^a-zA-Z]', string_to_split)

The [^a-zA-Z] part means "not alphabetic characters".

[^ a-zA-Z]部分表示“不是字母字符”。

#3


2  

You can use regex. The \W token will match all non-word characters (which is about the same as non-alphanumeric). Word characters are A-Z, a-z, 0-9, and _. If you want to match underscores as well you could just do [\W_].

你可以使用正则表达式。 \ W令牌将匹配所有非单词字符(与非字母数字字符大致相同)。单词字符是A-Z,a-z,0-9和_。如果你想匹配下划线,你可以做[\ W_]。

>>> import re
>>> line = '#   include"header.hpp"  ' 
>>> m = re.match(r'^\s*#\s*include\W+([\w\.]+)\W*$', line)
>>> m.group(1)
'header.hpp'

#4


1  

import re
s = 'foo bar- blah/hm.lala'
print(re.findall(r"\w+",s))

output : ['foo', 'bar', 'blah', 'hm', 'lala']

输出:['foo','bar','blah','hm','lala']

#5


1  

The two options mentioned by others that are best in my opinion are re.split and re.findall:

在我看来,其他人提到的两个选项是re.split和re.findall:

>>> import re
>>> re.split(r'\W+', '#include "header.hpp"')
['', 'include', 'header', 'hpp', '']
>>> re.findall(r'\w+', '#include "header.hpp"')
['include', 'header', 'hpp']

A quick benchmark:

快速基准:

>>> setup = "import re; word_pattern = re.compile(r'\w+'); sep_pattern = re.compile(r'\W+')"
>>> iteratiOns= 10**6
>>> timeit.timeit("re.findall(r'\w+', '#header foo bar!')", setup=setup, number=iterations)
3.000092029571533
>>> timeit.timeit("word_pattern.findall('#header foo bar!')", setup=setup, number=iterations)
1.5247418880462646
>>> timeit.timeit("re.split(r'\W+', '#header foo bar!')", setup=setup, number=iterations)
3.786440134048462
>>> timeit.timeit("sep_pattern.split('#header foo bar!')", setup=setup, number=iterations)
2.256173849105835

The functional difference is that re.split keeps empty tokens. That’s usually not useful for tokenization purposes, but the following should be identical to the re.findall solution:

功能上的区别在于re.split保持空令牌。这通常对标记化目的没有用,但以下内容应与re.findall解决方案相同:

>>> filter(bool, re.split(r'\W+', '#include "header.hpp"'))
['include', 'header', 'hpp']

#6


0  

While not exact, most parse header directives like this

虽然不精确,但大多数解析头指令都是这样的

(?m)^\h*#\h*include\h*["<](\w[\w.]*)\h*[">]

Where, (?m) is multi-line mode, \h is horizontal whitespace (aka [^\S\r\n] ).

其中,(?m)是多行模式,\ h是水平空格(又名[^ \ S \ r \ n])。

#7


0  

This works:

import re

test_str = '    #   include "header.hpp"'

match = re.match(r'\s*#\s*include\s*("[\w.]*")', test_str)
if match:
    print match.group(1)

推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了在使用Git进行版本控制时,如何提供类似CVS中自动增加版本号的功能。作者介绍了Git中的其他版本表示方式,如git describe命令,并提供了使用这些表示方式来确定文件更新情况的示例。此外,文章还介绍了启用$Id:$功能的方法,并讨论了一些开发者在使用Git时的需求和使用场景。 ... [详细]
  • 详解 Python 的二元算术运算,为什么说减法只是语法糖?[Python常见问题]
    原题|UnravellingbinaryarithmeticoperationsinPython作者|BrettCannon译者|豌豆花下猫(“Python猫 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 图像因存在错误而无法显示 ... [详细]
  • Python中的PyInputPlus模块原文:https ... [详细]
  • 概述H.323是由ITU制定的通信控制协议,用于在分组交换网中提供多媒体业务。呼叫控制是其中的重要组成部分,它可用来建立点到点的媒体会话和多点间媒体会议 ... [详细]
  • 我正在尝试将Firebase添加到涉及添加以下内容的现有应用程序中:classpath'com.googl ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • vue使用
    关键词: ... [详细]
  • 在Docker中,将主机目录挂载到容器中作为volume使用时,常常会遇到文件权限问题。这是因为容器内外的UID不同所导致的。本文介绍了解决这个问题的方法,包括使用gosu和suexec工具以及在Dockerfile中配置volume的权限。通过这些方法,可以避免在使用Docker时出现无写权限的情况。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
author-avatar
手机用户2502901575_836
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有