Let"s say I have a string
"gfgfdAAA1234ZZZuijjk" and I want to extract just the
I only know what will be the few characters directly before
AAA, and after
ZZZ the part I am interested in
sed it is possible to do something like this with a string:
echo "$STRING" | sed -e "s|.*AAA(.*)ZZZ.*|1|"
And this will give me
1234 as a result.
How to do the same thing in Python?
Using regular expressions - documentation for further reference
import re text = "gfgfdAAA1234ZZZuijjk" m = re.search("AAA(.+?)ZZZ", text) if m: found = m.group(1) # found: 1234
import re text = "gfgfdAAA1234ZZZuijjk" try: found = re.search("AAA(.+?)ZZZ", text).group(1) except AttributeError: # AAA, ZZZ not found in the original string found = "" # apply your error handling # found: 1234
We live in an age of so-called Big Data. We hear terms like data scientist, and there is much talk about analytics and the mining of large amounts of corporate data for tidbits of business value. Ther...
Taking into account the development of modern programming, especially the emerging programming languages that reflect modern practice, Numerical Programming: A Practical Guide for Scientists and...
Mark Lutz is the global leader in Python training, author of the oldest and best-selling Python texts, and a pioneer in the Python community since 1992.
Mark Lutz is the author of the found...
The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition). The Pragmatic Programmer is one of those rare technical books that you will read, reread, and re-read over...