99
votes
14answers
24k 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')
84
votes
10answers
28k 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: ...
47
votes
5answers
32k 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 (current documentation), but I never seem to remember it. I keep having to look ...
44
votes
3answers
3k 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
2answers
12k 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 ...
37
votes
10answers
30k 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 ...
34
votes
3answers
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 ...
34
votes
2answers
28k 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 = ...
32
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 ...
31
votes
4answers
68k 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 ...
31
votes
9answers
26k 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
470 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 ...
25
votes
2answers
1k 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> ...
24
votes
7answers
21k views

The SHORTEST way to remove multiple spaces in a string in Python

Suppose this is the string: The fox jumped over the log. It would result in: The fox jumped over the log. What is the simplest, 1-2 liner that can do this? Without splitting and going ...
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 ...

1 2 3 4 5 326
15 30 50 per page