Tagged Questions
94
votes
14answers
21k 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')
77
votes
10answers
27k 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:
...
44
votes
3answers
2k views
Worst Case Analysis for Regular Expressions
Are there any tools that will take a particular regular expression and return the worst case scenario in terms of the number of operations required for a certain number of characters that the regular ...
42
votes
5answers
29k views
What is the difference between Python's re.search and re.match?
What is the difference between the search() and match() functions in the Python re module?
I've read the documentation, but I never seem to remember it. I keep having to look it up and re-learn it. ...
40
votes
2answers
11k views
Escaping regex string in Python
I want to use input from a user as a regex pattern for a search over some text. It works, but how I can handle cases where user puts characters that have meaning in regex? For example, the user ...
33
votes
10answers
28k views
How do you validate a URL with a regular expression in Python?
I'm building a Google App Engine app, and I have a class to represent an RSS Feed.
I have a method called setUrl which is part of the feed class. It accepts a url as an input.
I'm trying to use the ...
33
votes
2answers
25k views
Case insensitive Python regular expression without re.compile
In Python, I can compile a regular expression to be case-insensitive using re.compile:
>>> s = 'TeSt'
>>> casesensitive = re.compile('test')
>>> ignorecase = ...
31
votes
8answers
3k views
Find the number of occurrences of a subsequence in a string
For example, let the string be the first 10 digits of pi, 3141592653, and the subsequence be 123. Note that the sequence occurs twice:
3141592653
1 2 3
1 2 3
This was an interview ...
30
votes
4answers
64k views
Regex replace (in Python) - a simpler way?
Any time I want to replace a piece of text that is part of a larger piece of text, I always have to do something like:
"(?P<start>some_pattern)(?P<replace>foo)(?P<end>end)"
And ...
30
votes
2answers
4k views
Does Flask support regular expressions in its URL routing?
I understand that Flask has the int, float and path converters, but the application we're developing has more complex patterns in its URLs.
Is there a way we can use regular expressions, as in ...
29
votes
9answers
23k views
In python how to I verify that a string only contains letters, numbers, underscores and dashes?
I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.
Thanks
26
votes
1answer
449 views
Library to check if two regular expressions are equal/isomorphic
I need a library which will take in two regular expressions and determine whether they are isomorphic (i.e. match exactly the same set of strings or not)
For example a|b is isomorphic to [ab]
As I ...
23
votes
12answers
6k views
Mass string replace in python?
Say I have a string that looks like this:
str = "The &yquick &cbrown &bfox &Yjumps over the &ulazy dog"
You'll notice a lot of locations in the string where there is an ...
23
votes
5answers
1k views
Find longest repetitive sequence in a string
I need to find the longest sequence in a string with the caveat that the sequence must be repeated three or more times. So, for example, if my string is:
...
23
votes
2answers
895 views
c++11 regex slower than python
hi i would like to understand why the following code which does a split string split using regex
#include<regex>
#include<vector>
#include<string>
std::vector<std::string> ...