Tagged Questions
0
votes
1answer
18 views
Escape sequences vs predefined character classes (aka special regex characters) when encapsulated by double quotes
Perl, like Java and Python, has \s, the special regex character that matches whitespace, in addition to other special characters.
In Perl, the following would not be valid:
my $sentence = "The ...
1
vote
2answers
43 views
identifying position of the pattern match
I need to find the exact position where the string matched..
>>> pattern = 'Test.*1'
>>> str1='Testworld1'
>>> match = re.search(pattern,str1)
>>> match.group()
...
2
votes
2answers
47 views
Python regex: how to match strings that DO NOT contain an *exact* sentence?
I want to filter out messages from a log file that contain e.g. the sentence This is message 12345. Ignore.
If I would use grep, I could simple pass the sentence and use the -v switch, for example:
...
2
votes
3answers
54 views
Python Re, need a list of matches
Say we have a string, '123A......'. I have a collection of other strings that also begin with '123A......' where after the A are other letters of the alphabet, for example, '123AA.....' and ...
1
vote
2answers
47 views
Random String Generation Based on Regular Expression-Python
According to the this random strings can be generated importing rstr module.
import rstr
rstr.rstr('ABC')
but when I compile this following error is given?
ImportError: No module named rstr
I'm ...
1
vote
3answers
68 views
Replace sequence of same characters
What is the fastest way in Python to replace sequence of 3 and more same characters in utf-8 text?I need to replace sequence of 3 and more same characters with exact 2 characters.
I.e.
aaa -> aa
...
2
votes
1answer
45 views
Beautiful Soup Type Error and Regex
I am trying to find all the emails on a given page and match them using a regex. I am using BeautifulSoup to get all the tags
email_re = re.compile('[A-Za-z0-9\.\+_-]+@[A-Za-z0-9\._-]+\.[a-zA-Z]*')
...
0
votes
4answers
1k views
Regular expression dictionary in python
Is it possible to implement a dictionary with keys as regular expressions and actions (with parameters) as values?
for e.g.
key = "actionname 1 2", value = "method(1, 2)"
key = "differentaction ...
2
votes
4answers
1k views
Algorithm to extract network info from ifconfig (ubuntu)
Im trying to parse info from ifconfig (ubuntu). Normally, I would split a chunk of data like this down into words, and then search for substrings to get what I want. For example, given line = "inet ...
1
vote
1answer
699 views
Efficient way to read/write/parse large text files (python)
Say I have an absurdly large text file. I would not think my file would grow larger than ~500mb, but for the sake of scalability and my own curiosity, let's say it is on the order of a few gig.
My ...
3
votes
4answers
2k views
How to use variables in Python regular expression
i am in need to use a variable in python regular expression
for line in re.findall('(.+)33084-2(.+)', Data):
i am using above code to match a line with 33084-2, some times this values changes so i ...
13
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, ...
3
votes
2answers
888 views
python regular expression
whats the difference between '{m}' and '{m,n}?' in http://docs.python.org/library/re.html it says '{m,n}?' matches numbers in range m to n times, but it is not a greedy search. Therefore if its not a ...
9
votes
1answer
4k views
How can I find all matches to a regular expression in Python?
In a program I'm writing i have python use the re.search() function to find matches in a block of text and print the results. However, once the program finds the first match in the block of text, it ...
18
votes
3answers
28k views
Python: Extract numbers of a string
I would extract all the numbers contained in a string. Which is the better suited for the purpose, regular expressions or the isdigit() method?
Example:
line = "hello 12 hi 89"
Result:
[12, 89]