[[+content_image]]
A
A
Anton2015-12-09 16:43:06
Python
Anton, 2015-12-09 16:43:06

Why does the string not match the regular expression?

Pattern: ^.*\[[^\d,]*\].*$
String: anything[2287172,1932347]anything
You need to leave only the contents of the [] brackets, but the string does not change according to this pattern. Why?

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexey Yarkov, 2015-12-09
@Gambetto

Because it needs to be:

import re

string = 'anything[2287172,1932347]anything'
pattern = re.compile('^.+\[([\d,]+)\].+$')
string = pattern.match(string).group(1)
print string

A
alex stephen, 2015-12-09
@berezuev

^.+\[([\d,]+)\].+$
If you need to split the numbers, then through split

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question