Как делают меня, regex соответствуют группировке с неизвестным количеством групп

gnulib, библиотека мобильности гну.

Это распределяется как исходный код. Этот список от список модулей , который включает ТОННУ других вещей. Один интересный является "c-стеком: обработка Переполнения стека, вызывая выход программы".

  • список
  • carray-список списка массива
  • связанный список
  • avltree-список
  • rbtree-список
  • linkedhash-список
  • avltreehash-список
  • rbtreehash-список
  • подсписок (Последовательный тип данных списка, поддержанный другим списком.)
  • oset (Абстрактное упорядоченное множество.)
  • массив-oset
  • avltree-oset
  • rbtree-oset

25
задан Lorin Hochstein 10 September 2009 в 20:06
поделиться

3 ответа

What you're looking for is a parser, instead of a regular expression match. In your case, I would consider using a very simple parser, split():

s = "VALUE 100 234 568 9233 119"
a = s.split()
if a[0] == "VALUE":
    print [int(x) for x in a[1:]]

You can use a regular expression to see whether your input line matches your expected format (using the regex in your question), then you can run the above code without having to check for "VALUE" and knowing that the int(x) conversion will always succeed since you've already confirmed that the following character groups are all digits.

21
ответ дан 28 November 2019 в 21:28
поделиться
>>> import re
>>> reg = re.compile('\d+')
>>> reg.findall('VALUE 100 234 568 9233 119')
['100', '234', '568', '9223', '119']

That doesn't validate that the keyword 'VALUE' appears at the beginning of the string, and it doesn't validate that there is exactly one space between items, but if you can do that as a separate step (or if you don't need to do that at all), then it will find all digit sequences in any string.

11
ответ дан 28 November 2019 в 21:28
поделиться

You could just run you're main match regex then run a secondary regex on those matches to get the numbers:

matches = Regex.Match(log)

foreach (Match match in matches)
{
    submatches = Regex2.Match(match)
}

This is of course also if you don't want to write a full parser.

2
ответ дан 28 November 2019 в 21:28
поделиться
Другие вопросы по тегам:

Похожие вопросы: