Agile Spain 2010 links from Twitter
![]()
Hi, here are all the the links collected from #agilespain2010 posted in Twitter after applying this regex. ^(.*)(http://\S+)(.*)$ plus ordering + plus filtering within a small script.
![]()
Hi, here are all the the links collected from #agilespain2010 posted in Twitter after applying this regex. ^(.*)(http://\S+)(.*)$ plus ordering + plus filtering within a small script.
The problem: You match a string with your regex, but you need to replace just a portion of it. How could we replace it?
The trick is simple, put the text you want to replace within “()” which means “group” in regex language. If the regex worked, you could replace just that portion by using Python match information, like in this example:
#the first group contains the expression we want to replace pat = "word1\s(.*)\sword2" test = "word1 will never be a word2" repl = "replace" import re m = re.search(pat,test) if m and m.groups() > 0: line = test[0:m.start(1)] + repl + test[m.end(1):len(test)] print line else: print "the pattern didn't capture any text"
This will print: ‘word1 will never be a word2‘
The group to be replaced could be located in any position of the string.
Task: Parse a file and capture whatever text appears between a pair of double quotes like the following:
“Catch me”
Not so difficult, you could use the following regex:
“.*”
This will catch any character within double quotes in a group
¿any? Read more…