python - Regular Expressions: Special Characters and Tab Spaces -
i testing out function wrote. supposed give me count of full stops (.) in line or string. full stop (.) interested in counting has tab space before , after it.
here have written.
def seek(): = '1 . . 3 .' b = a.count(r'\t\.\t') return b seek()
however, when test it, returns 0. a, there 2 full stops (.) both tab space before , after it. using regular expressions improperly? represented incorrectly? appreciated.
thanks.
it doesn't a
has tabs in it. although may have hit tab
key on keyboard, character have been interpreted text editor "insert number of spaces align next tab character". need line this:
a = '1\t.\t.\t3\t.'
that should it.
a more complete example:
from re import * def seek(): = '1\t.\t.\t3\t\.' re = compile(r'(?<=\t)\.(?=\t)'); return len(re.findall(a)) print seek()
this uses "lookahead" , "lookbehind" match tab
character without consuming it. mean? means when have \t.\t.\t
, match both first , second \.
. original expression have matched initial \t\.\t
and discarded them. after, there have been \.
nothing in front of it, , no second match. lookaround syntax "zero width" - expression tested ends taking no space in final match. thus, code snippet gave returns 2
, expect.
Comments
Post a Comment