Tagged Questions
0
votes
0answers
3 views
replace a string with regular expression in python
I have been learning regular expression for a while but still find it confusing sometimes
I am trying to replace all the
self.assertRaisesRegexp(SomeError,'somestring'):
to
...
0
votes
1answer
15 views
Python Looping Through A Directory Using a Regex Printing Only The Desired Results
Being new to python, I have two files populated as such in a directory called test-caca in which I would like to loop through each of these files, searching for an ip address using a regular express ...
-1
votes
2answers
58 views
Python find and remove between tags
I have some code in a string;
/*
* REMOVE ME
*
* */
blah
more blah
/*
* REMOVE ME
*
* */
Using python 2.7 I need to remove the text between the tags.
Thanks
0
votes
2answers
54 views
How to return a list of words specified by regular expression from file in Python [on hold]
so I have a file containing a list of words with each word on a new line.
how can i write a function that takes a filename and a regular expression as input and returns the list of words with in the ...
-1
votes
1answer
29 views
Python, Using regex to read a password
I am aware that you can use the pwd module on python to extract passwd structures for use; however, my question is as such:
if I read into my program a line such as
...
2
votes
3answers
51 views
Python regex string expansion
Suppose I have the following string:
trend = '(A|B|C)_STRING'
I want to expand this to:
A_STRING
B_STRING
C_STRING
The OR condition can be anywhere in the string. i.e STRING_(A|B)_STRING_(C|D)
...
0
votes
2answers
26 views
Python RegEx help: Splitting string into numbers, characters and whitespace
I am trying to split my string into a list, separating by whitespace and characters but leaving numbers together.
For example, the string:
"1 2 +="
would end up as:
["1", " ", "2", " " ,"+", ...
1
vote
5answers
46 views
Replace any number of white spaces with a single white space
Is there a way to use replace with a regex denoting any number of any white space (blank but also tab) with something? I am trying the following to contract any extension of multiple white space to ...
1
vote
2answers
39 views
Python regex pattern definition excluding a character
I am writing a simple Java source file parser in Python. The main objective is to extract a list of method declarations. A method starts with public|private|protected (I assume there are no friendly ...
-1
votes
2answers
28 views
Not getting expected results using findall in python
I am new to python(using 2.7.3). I was trying to do web scraping using python but I am ot getting the expected outputs:
import urllib
import re
regex='<title>(.+?)<\title>'
...
0
votes
1answer
63 views
Regex negative matching with python
I have a string which is allowed to contain only alphabets, numbers, empty spaces and symbol ':'. So I wrote the following code :
regex = r'![a-zA-Z0-9\:\ ]+'
print re.match(regex, myString)
...
0
votes
0answers
39 views
django regex error: nothing to repeat
I generally understand that the message error: nothing to repeat means there is an issue the some regular expression. However I have been unable to find out where the error is. Below is the regexs in ...
0
votes
3answers
50 views
regex pattern in python for parsing HTML title tags
I am learning to use both the re module and the urllib module in python and attempting to write a simple web scraper. Here's the code I've written to scrape just the title of websites:
...
1
vote
1answer
27 views
Confusion escaping single quotes in a single-quoted raw string literal
The following works as expected:
>>> print re.sub('(\w)"(\W)', r"\1''\2", 'The "raw string literal" is a special case of a "string literal".')
The "raw string literal'' is a special case of ...
1
vote
2answers
47 views
Split a string in python with spaces and punctuations mark , unicode characters , etc.
I want to split string like this:
string = '[[he (∇((comesΦf→chem,'
based on spaces, punctuation marks also unicode characters. I mean, what I expect in output is in following mode:
out= ['[', ...
-2
votes
4answers
41 views
What is the regex to remove the content inside brackets?
I want to do something like this,
Alice in the Wonderland [1865] [Charles Lutwidge Dodgson] Rating 4.5/5
to
Alice in the Wonderland Rating 4.5/5
What is the regex command to achieve this ?
0
votes
1answer
28 views
What does the \W mean in pattern re.U?
I have an text file named gugong.txt with some Chinese and English words in it.I want to extract the Chinese words out . the code is below:
note_file = open('gugong.txt','rb')
note_file = ...
-1
votes
1answer
29 views
Python Regex Find Pattern, Remove Rest of String, Write new String to File
I have the codez:
import re
pattern = ','
firstNames = "dictionary//first_names.txt"
new_file = []
def openTxtFile(txtFile):
file = open (txtFile,"r")
data = file.read()
print (data)
...
2
votes
1answer
34 views
Python complicated regex string expansion
Suppose I have a string of the following form:
ABCDEF_(0-100;1)(A|B)_GHIJ_(A-F)
I want to be able to expand this to:
ABCDEF_0A_GHIJ_A
ABCDEF_1A_GHIJ_A
ABCDEF_2A_GHIJ_A
...
ABCDEF_100A_GHIJ_A
...
0
votes
2answers
24 views
Parametrizing a Date on urls.py in Django
I have the following URL definition:
url(r'^date-add/(?P<entity_id>\d+)$', views.date_add, name='date_add'),
That allows me to call date_add function with the following URL:
...
3
votes
1answer
32 views
Regex words with hyphen
I am looking for a regex to match words (no spaces in them) that have at least one number, one letter (a-zA-Z), and at least one hyphen. And the total size should be between 4 and 40.
Examples:
hi ...
2
votes
4answers
56 views
How to get values from similar strings in Python?
Suppose I have the following strings, from a file containing similar strings :
Andorra la Vella|ad|Andorra la Vella|20430|42.51|1.51|
Canillo|ad|Canillo|3292|42.57|1.6|
...
0
votes
1answer
32 views
string split with python regex [on hold]
An updated question to my previous one Python string split using regex, I'm trying to parse lines like:
123foo bar456 baz
123foo, bar456, baz
123foo > 13.0 bar456 = 1024 baz
123foo > 13.0, ...
1
vote
3answers
52 views
Python string split using regex
I need to parse a line like these:
foo, bar > 1.0, baz = 2.0
foo bar > 1.0 baz = 2.0
foo, bar, baz
foo bar baz
for each element it can be $string (>|<|<=|>=|=) $num or just $string, ...
1
vote
0answers
29 views
IRC Client in Python; not a IRC Bot
I've searched extensively on this and only came up with bot specific clients. I know they're basically the same, but for what I want to do I just can't figure it out.
I am trying to program a python ...
1
vote
3answers
34 views
How to use re.findall to get only strings with lowercase letters in python
Suppose if my list has ["Apple","ball","caT","dog"]
then it should give me result 'ball and 'dog'.
How do I do that using re.findall()?
0
votes
4answers
43 views
Python re.findall
I'm trying to retrieve all the tags containing a 'name' field, and then treat the whole sentence plus the name.
This is the test code I have:
sourceCode = '<dirtfields name="one" ...
2
votes
3answers
39 views
Extract words between the 2nd and the 3rd comma
I am total newbie to regex, so this question might seem trivial to many of you.
I would like to extract the words between the second and the third comma, like in the sentence:
Chateau d'Arsac, ...
2
votes
1answer
28 views
regex with python error
I am learning python, and trying to use regex. I am used to do that with shell script (awk, grp and sed), but need to do that with python.
in my file, I have lines like:
species,subl,cmp= 1 7 ...
1
vote
3answers
43 views
Multi-line dictionaries: Replace the key as per a word in value
I have a dictionary in which I have to replace all the keys depending on a word in the value set. So my dictionary is:
{ 23: {'score': -8.639, 'char': False, 'word': 'positive'} }
{ 56: ...
1
vote
1answer
29 views
Python Regex - Find contents from a string between two '*'
I have a text file, and I need to extract everything from the file between two '*'s. There can be multiple occurrences of the same. How would I do that using Regex? I am good at Python, but I haven't ...
1
vote
2answers
58 views
Python - regex to remove quotes from entire line in CSV file [on hold]
I've seen a lot on here about how to remove quotes from within a line in a CSV file, but that's not quite the problem I have. Using something like
with open(path), 'wb') as myfile:
writer = ...
2
votes
2answers
38 views
Find 1 letter and 2 numbers using RegEx
I have been writing a program recently and a part of it requires me to get information form inside a string. I need to find where there is 1 letter immediately followed by 2 numbers (e.g. S07) and I ...
-1
votes
1answer
42 views
finding a word in a string without spaces [on hold]
I have a string without space. eg system-gnome-theme-60.0.2-1.el6.
I have to check in 100 other such strings (without space) which have a few of the previously specified words; e.g. gnome, samba.
...
0
votes
1answer
78 views
Parenthesis matching code in python
I have written following code and it was working fine.But One of the case it is failing.I tried but not able to fix this issue.
#!/usr/bin/env py
import itertools
import sys
import sympy
import re
...
0
votes
1answer
33 views
Python Regex match multiline Java annotation
I am trying to take advantage of JAXB code generation from a XML Schema to use in an Android project through SimpleXML library, which uses another type of Assertion than JAXB (I do not want to include ...
6
votes
2answers
58 views
Get start and stop indexes of overlapping matches?
I need to know start and end indexes of matches from next regular expression:
pat = re.compile("(?=(ATG(?:(?!TAA|TGA|TAG)\w\w\w)*))")
Example string is s='GATGDTATGDTAAAA'
pat.findall(s) returns ...
0
votes
3answers
33 views
Find a line ends up with '-' in regular expression python
I am trying to locate the lines which end up with '-' in a text file. I used the following expression but not working. I am not familiar with regex. Can someone help me? thank you!
if ...
0
votes
1answer
29 views
Why does python regex seem to fail to match beyond 112 bytes?
I have a file, what.dmp, which is 116 bytes long. And my python code looks like this:
import binascii
import re
import sys
print(sys.version)
needle = re.compile(b".{112}")
with open("what.dmp", ...
0
votes
3answers
46 views
regex negative lookahead assertion doesn't seem to work
Why does my pattern produce this result? I expect it to find ATG then a sequence of 3 which does not include TAA.
In [102]: s = 'GATGCCTAAG'
In [103]: pat = re.compile("(ATG((\w\w\w)*)(?!TAA))")
In ...
0
votes
0answers
21 views
Django mongoengine raw query $in with regex
I'm trying to write a $in query with regex using MongoDB and Django/Mongoengine ODM. I need to execute a query that looks like:
db.ad.find({
'place_id':{'$in': ...
1
vote
3answers
111 views
Regex to match all sentences with quotes in them
I am trying to match all sentences that contain quotes, independent of the length of the quote or the number of sentences within the quote.
As Alfe point out, getting a perfect regex is maybe not ...
-2
votes
2answers
37 views
How to convert strings to regular expressions in python? [closed]
I get some strings passed from command line arguments in my python script and want to use these strings as regular expressions. How to do?
0
votes
2answers
31 views
Match all characters except a few using .(dot) in multiline string using Regex
My input string is as follows :
The dog is black
and beautiful
The dog and the cat
is black and beautiful
I want to replace 'black' to 'dark' only when the cat is not described .
So my output ...
0
votes
1answer
27 views
Regular Expression for alpha/numeric characters, spaces and dashes [closed]
I need some help writing a couple of complex regular expression that are way over my head.
The first Regex, I want to exclude everything except:
The letters A to Z in both upper and lowercase
...
-3
votes
1answer
52 views
check if string contains only alphabets in python
I have strings in this form: string space string space string
, test ,
test test1 test2
[ test test1
What I want is for all the three strings to only have alphabets, and if it does not then return ...
-1
votes
2answers
99 views
Parenthesis matching in Python
I have string in following way. I need to Extract string from next state and add matching
parenthesis. Do we have function in Python which can do parenthesis matching in Python
a) data = ...
2
votes
1answer
54 views
Retain only specified content in a string
I have data in the following form in a file:
<string1> abc:string2 ...
1
vote
1answer
44 views
How can I substitute an expression {{ text }} with re.sub() when 'text' may include further {{ text }} blocks?
I'm trying to parse raw wikipedia article content, e.g. the article on Sweden, using re.sub(). However, I am running into problems trying to substitute blocks of {{some text}}, because they can ...
1
vote
1answer
55 views
How to write a regular expression to search such string? [on hold]
_stocksearch_callback([{"type":"SZ","symbol":"000001","name":"a","spell":"PAYH"},{"type":"SH","symbol":"000001","name":"b","spell":"SZZS"},{"type":"FN","symbol":"000001","name":"c","spell":"HXCZHH"}])
...