ich habe vor jahren schonmal mit python rumgespielt und jetzt muss ich mal wieder etwas coden und habs wieder ausgekramt.
und bin auf dieses schöne forum gestossen. hallo
natürlich habe ich auch gleich ein problem
ich nutze eine sehr unkomfortable sprache für die ich in python textersetzungsmakros schreiben will.
das wichtigste wäre erstmal eine art printf() zu haben.
Code: Alles auswählen
//ich würde also folgenden kommentar in den code schreiben
//MACRO:PRINTF("hello %I cruel %L world", 49, 11)
//der dann vom py-skript in dieses umgewandelt wird:
//den folgenden code könnt ihr ignorieren, aber wenn ihr mein leid wirklich nachvollziehen wollt ...
int printf_ints[1];
printf_ints[0] = 49;
long printf_longints[1];
printf_longints[0] = 11;
string printf_msg;
printf_msg = "hello %I cruel %L world";
printf(printf_msg, printf_ints, printf_longints);
Code: Alles auswählen
test_data = '//MACRO:PRINTF("hello %I cruel %L world",49,11)'
p_full = re.compile('''
//MACRO:PRINTF # flag
\(" # arguments
(
[a-zA-Z ]+ # string
|
(?P<FMT_INT>%I) # format int
|
(?P<FMT_LONG>%L) # format long
)+
([, ]|(?P<ARG>[0-9]+))* # comma separated (long)int arguments
"\)
''', re.VERBOSE)
aber:
komme ich nicht an eine liste mit allen matches für eine gruppe?group( [group1, ...])
Returns one or more subgroups of the match. (...) If a group is contained in a part of the pattern that matched multiple times, the last match is returned.
muss ich das also irgendwie folgendermaßen machen:
Code: Alles auswählen
>>> p_divide = re.compile('//MACRO:PRINTF\("(?P<STRING>.*?)"(?P<ARGS>[, 0-9]*)\)
>>> p_arg = re.compile("(?P<INT>%I)|(?P<LONG>%L)")
>>>
>>> m = p_divide.match('//MACRO:PRINTF("hello %I cruel %L wor %I ld",49,11,33)')
>>>
>>> s = m.group("STRING")
>>> m = p_arg.search(s)
>>> while m:
... pos = m.start()
... s = s[pos+1:]
... print s[0]
... m = p_arg.search(s)
...
I
L
I