regex - Python - How to split a string by non alpha characters -
i'm trying use python parse lines of c++ source code. thing interested in include directives.
#include "header.hpp"
i want flexible , still work poor coding styles like:
# include"header.hpp"
i have gotten point can read lines , trim whitespace before , after #. still need find out directive reading string until non-alpha character encountered regardless of weather space, quote, tab or angled bracket.
so question is: how can split string starting alphas until non alpha encountered?
i think might able regex, have not found in documentation looks want.
also if has advice on how file name inside quotes or angled brackets plus.
you can regex. however, can use simple while
loop.
def splitnonalpha(s): pos = 1 while pos < len(s) , s[pos].isalpha(): pos+=1 return (s[:pos], s[pos:])
test:
>>> splitnonalpha('#include"blah.hpp"') ('#include', '"blah.hpp"')
Comments
Post a Comment