String manipulation advice needed.

Fredrik Lundh fredrik at pythonware.com
Wed Oct 13 22:26:23 CEST 2004


"Raaijmakers, Vincent (GE Infrastructure)" wrote:

> What is the easiest way of getting this information out of a string:

making some reasonable assumptions, and generalising to any number
of information instances:

> foo = "My number 70 is what I want to parse"  => 70

print re.findall("\d+", foo)

> foo = "My info {info} between the curly brackets is what I want to parse" => {info}

print re.findall("{[^}]+}", foo)

> foo = "My  info between [hello world] is what I want to parse" => [hello world]

print re.findall("\[[^]]+\]", foo)

or, as a one-size-fits-all pattern:

print re.findall("\d+|{[^}]+}|\[[^]]+\]", foo)

</F> 






More information about the Python-list mailing list