Tagged Questions
102
votes
15answers
25k views
Is it worth using Python's re.compile?
Is there any benefit in using compile for regular expressions in Python?
h = re.compile('hello')
h.match('hello world')
vs
re.match('hello', 'hello world')
86
votes
10answers
29k views
Split a string by spaces — preserving quoted substrings — in Python
I have a string which is like this:
this is "a test"
I'm trying to write something in Python to split it up by space while ignoring spaces within quotes. The result I'm looking for is:
...
14
votes
5answers
2k views
Type of compiled regex object in python
What is the type of the compiled regular expression in python?
In particular, I want to evaluate
isinstance(re.compile(''), ???)
to be true, for introspection purposes.
One solution I had was, ...
11
votes
6answers
12k views
How to input a regex in string.replace in python?
I need some help on declaring a regex. My inputs are like the following:
this is a paragraph with<[1> in between</[1> and then there are cases ... where the<[99> number ranges from ...
2
votes
1answer
317 views
regular expression \Z(?ms)
What does \Z(?ms) mean in a regular expression?
\Z is end-of-string, but what is the (?ms) part?
This is added by fnmatch.translate("abc") to the returned regular expression.
2
votes
3answers
296 views
string convert with python re
I get a string line:
>>> line = " abc\n def\n\n ghi\n jkl"
>>> print line
abc
def
ghi
jkl
and I want to convert it to "abcdef\n\n ghijkl", like:
>>> print ...
1
vote
3answers
7k views
Python regex search - 'wild card' matching a string
thought i would write some quick code to download the number of 'fans' a page had of fb, just b/c i thought would be interested to get a handle on the regex search in python.
for some reason despite ...
1
vote
2answers
31 views
regex match and replace multiple patterns
I have a situation where a user submits an address and I have to replace
user inputs to my keys. I can join this using an address without suffixes.
COVERED WAGON TRAIL
CHISHOLM TRAIL
LAKE TRAIL
...
1
vote
3answers
5k views
Python finding substring between certain characters using regex and replace()
Suppose I have a string with lots of random stuff in it like the following:
strJunk ="asdf2adsf29Value=five&lakl23ljk43asdldl"
And I'm interested in obtaining the substring sitting between ...
1
vote
3answers
511 views
Python: Export entire regex group to text file
When I print the group "print(a)" the entire group is shown. When I save it to a text file "open("sirs1.txt", "w").write(a)" only the last row is saved to the file.
import re
def main():
f = ...
1
vote
1answer
34 views
CSV read/write using regex re.sub
I'd like to read a CSV file and recompile using:
re.sub('\s+(STREET|ST|TRAIL|TRL|TR)\s*$', '', test_file, flags=re.M)
I'm getting:
TypeError: expected string or buffer
When using:
import csv
...
0
votes
2answers
43 views
Python parentheses and returning only certain part of regex
I have a list of strings that I'm looping through. I have the following regular expression (item is the string I'm looping through at any given moment):
regularexpression = re.compile(r'set(\d+)e', ...
0
votes
2answers
29 views
Find sublist of strings that match a regex that requires ordered inclusion from another list
Say I have a list lst with dozens of thousands of strings. Say also that I have a list of strings
strings_to_match, e.g.:
strings_to_match = ['foo', 'bar', 'hello']
I would like to find strings ...
0
votes
4answers
30 views
Extract java script from html document using regular expression
I am trying to extract java script from google.com using regular expression.
Program
import urllib
import re
gdoc = urllib.urlopen('http://google.com').read()
scriptlis = ...
0
votes
1answer
43 views
reading $, . and number in python regex
I want to be able to read the price in a given line, for example:
$.0001 ...
I'm trying to read the just the $.0001 part with the following regex:
new_string = re.search(r'\$([.]\d+)', ...