Регулярное выражение для парсинга разграниченных пробелом данных

Client Джанго расширяет RequestFactory , поэтому вы должны иметь возможность передавать дополнительные параметры в качестве аргументов ключевых слов.

Попробуйте:

response = self.c.get('/emails/html/upload', SERVER_NAME="mydomain.com")

1
задан Brad Mace 10 July 2011 в 05:01
поделиться

4 ответа

Я бы сделал это с помощью следующих выражений:

(?-s)(\S+) +(.+)

и

(?-s)(.{11})(\D+)(.+)


И разбитых в режиме комментариев регулярных выражений, это:

(?x-s)    # Flags: x enables comment mode, -s disables dotall mode.
(       # start first capturing group
 \S+     # any non-space character, greedily matched at least once.
)       # end first capturing group
[ ]+     # a space character, greedily matched at least once. (brackets required in comment mode)
(       # start second capturing group
 .+      # any character (excluding newlines), greedily matched at least once.
)       # end second capturing group

и

(?x-s)    # Flags: x enables comment mode, -s disables dotall mode.
(       # start first capturing group
 .{11}   # any character (excluding newlines), exactly 11 times.
)       # end first capturing group
(       # start second capturing group
 \D+     # any non-digit character, greedily matched at least once.
)       # end second capturing group
(       # start third capturing group
 .+      # any character (excluding newlines), greedily matched at least once.
)       # end third capturing group


(режим 'dotall' ( flag s ) означает, что . соответствует всем символам, включая символы новой строки, поэтому мы должны отключить его, чтобы предотвратить слишком много совпадений в последней группе.)

2
ответ дан 3 September 2019 в 01:22
поделиться

Я бы использовал регулярные выражения Питера Боутона, но убедитесь, что у вас есть. соответствует отключенной новой строке. Если он включен, убедитесь, что вы добавили $ в конце :)

Жадные регулярные выражения будут работать лучше.

0
ответ дан 3 September 2019 в 01:22
поделиться

Самый простой способ представления данных, которые вы представляете, - разбить строку на поля в пробелах, а затем воссоединить то, что вы хотите, вместе. Regex.Split (строка, "\\ s +") должен возвращать массив строк. Это также более устойчиво к изменению строк в полях, например, если во втором случае строка читает «00006011731 TAB 3FC 10MG 30UOU».

-1
ответ дан 3 September 2019 в 01:22
поделиться

Supposing you know how to handle the VB.NET code to get the groupings (matches) and that you are willing to strip the extra spaces from the groupings yourself

The Regex for case 1 is

(.*?\s+)(\d+.*)
    .*? => grabs everything non greedily, so it will stop at the first space
    \s+ => one or more whitespace characters

    These two form the first group.

    \d+ => one or more digits
    .* => rest of the line

    These two form the second group.

The Regex for case 2 is

(.{11})(.*?)(\d.*)
    .{11} => matches 11 characters (you could restrict it to be just letters
             and numbers with [a-zA-Z] or \d instead of .)

    That's the first group.

    .*? => Match everything non greedily, stop before the first 
           digit found (because that's the next regex)

    That's the second group.

    \d.* => a digit (used to stop the previous .*?) and the rest of the line

    That's the third group.
1
ответ дан 3 September 2019 в 01:22
поделиться
Другие вопросы по тегам:

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