热门标签 | 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)

推荐阅读
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社区 版权所有