Tagged Questions
0
votes
2answers
42 views
Finding all instances of a pattern in a string in python
I'm writing a command parser for an account storage tool in python and have the basics down fine.
the user enters a command into the programs interpreter (not command line args) like [+] -u peter -e ...
0
votes
1answer
32 views
python3-invalid syntax on a line that do not exist
python3-invalid syntax error
File "6_1.py", line 8
SyntaxError: invalid syntax
But there is no line 8. So what is the problem.
import zipfile,re
with ZipFile('../../下载/channel.zip') as myzip:
...
0
votes
2answers
48 views
python regular expressions in find and replace
am trying to write a regular expression to insert a space in specific location. Am reading html file and trying to insert a space between #WORD<tag so it would be #WORD <
where WORD is a ...
1
vote
3answers
42 views
In python, how to do regex which catches an url in a <a href tag?
I am trying to make a regex in Python which catches an url in a :
<a href tag
For example, if i take this :
<a ...
4
votes
2answers
39 views
Python regex to match begin of string or whitespace
I have the following string
'abc[123]defgh ijk[456]lm no[78] pq'
And I would like to extract all parts which are either between the begin of the string and [ or between whitespace and [. For the ...
1
vote
3answers
29 views
Trying to catch one large multiline block in python
I'm new to python and have problems catching a multiline text block
on a raspberry pi running python.
I'm trying to catch the multiline text between HELLO and WORLD.
This example throws the error
...
0
votes
1answer
38 views
Regex doesn`t find matches
I am fairly new to using regex and am trying to find the following text :
<div class="name">
<a ...
1
vote
3answers
33 views
how to change internal sorting system of glob module in python
After applying a regex to the namefiles of a directory which start by 'chr[0-9XY]'*, I obtain a list in the following order:
['chr9', 'chr8', 'chr7', 'chr6', 'chr5', 'chr4', 'chr3', 'chr2', ...
-4
votes
1answer
36 views
Capitalization of the first letters of words in a sentence using python code
The code below capitalizes the first word of the words in "w".I would like to know how the function captilize(sentence) works? Specifically what does sentence.split do?
`
import random
def test():
...
-2
votes
3answers
49 views
Converting a Ruby Regular Expression to Python
I don't know much about Ruby, I have this Regular Expression in Ruby:
newline.gsub!(/\s+(((زا(ی)?)?|ام?|ات|اش|ای?(د)?|ایم?|اند?)[\.\!\?\،]*)\s+/,'\1 ')
What does it do, and if possible what is its ...
-1
votes
1answer
17 views
Why does 'u' appear in front of my strings? [duplicate]
I'm newbie to scrapping the web.
I've scrapped this table with BeautifulSoup in Python but the simbol u' appears and it appears neither the table nor its code.
The code is the following :
import ...
0
votes
3answers
28 views
Extract all domains from text
I need to extract domains from a string. I have a valid regex, that has been tested however I cannot get it to work with the following code. Probably something obvious that I'm missing here
mytext = ...
0
votes
3answers
75 views
Using Regular Expression in Python to determine the type of geometry
I have a number of MULTIPOLYGONS in the form of WKT (Well Known Text) like this:
A single multipolygon with no holes:
MULTIPOLYGON(((11 -17, -8 -1, 14 -8, 18 -17, 3 -11, 0 18, -17 -12, -17 -10, -10 ...
1
vote
2answers
27 views
Python regex, remove all punctuation except hyphen for unicode string
I have this code for removing all punctuation from a regex string:
import regex as re
re.sub(ur"\p{P}+", "", txt)
How would I change it to allow hyphens? If you could explain how you did it, ...
0
votes
1answer
33 views
Capture multiple words in group
I am trying to parse something in Python where I have a string, a number, and then an undetermined amount of strings after the number. I would like to be able to put all the remaining strings (in this ...
1
vote
2answers
18 views
Python re.sub to change only the word 'a' as opposed to every instance of 'a' as a letter
For example, let's say I want to change every word 'a' into 'an' in the following text:
"a apple is a| awesome fruit."
Assume that the "|" character is there as a garbage character that needs to be ...
0
votes
2answers
42 views
Python - Scraping website data - formatting printed results
I have been experimenting on python 2.7.3 to extract data from an RSS feed so I can output it to the python shell.
I found a video on you tube on the subject and copied the code from their tutorial.
...
0
votes
3answers
50 views
Python Regex Syntax
I have the following function:
def handleRequest(self, command, ident, ip, duration=0):
if not re.match("^[0-9]+$", ident) or not re.match("^[0-9.]+$", ip) or \
(duration and not ...
1
vote
2answers
35 views
Customizing Regex for Validator in Python
Just came across a line of code that enforces an user's text input into the dialog's field. Regardless of the input only IP format will be allowed at the end such as :
123.456.789.100
...
1
vote
2answers
31 views
Regex-Match password in mail URL
Assuming I have an SMTP/IMAP/POP3 login URL like this:
smtp://[email protected]:[email protected]:465
I want to replace the password (abc in this case) by a constant number of stars (e.g. ...
-1
votes
4answers
56 views
Matching email regex [on hold]
SO I'm trying to match an email of this form
[email protected]
I've come up with this regex
(\w+(?=-))*-@-(\w+(?=-))*.(\w+(?=-))
Why is that not working?
0
votes
0answers
26 views
Different Languages for django username
How to enable users to use other languages such as Chinese in username field using the usercreation form?
The methods I have tried:
modify the regex field of username field, failed
What it tells ...
4
votes
3answers
45 views
re.sub only replacing two occurances [duplicate]
I am using re.sub but it is not replacing all occurences.
re.sub(r'\b\s+\b', '.', string, re.I)
The first is the input and the second is the output.
a b c d e f g
a.b.c d e f g
0
votes
3answers
27 views
Changes in re module between Python 2 and Python 3
I am running my unit test suit with python3.x on a code that was developed under python2.x. All unit tests passed under python2.x but not for python3.x. It seems there is some change in the ...
0
votes
1answer
27 views
Grabbing brackets and everything inside Regex
I am reading a line that has the following format
.... .... [..*stuff*..].... ... .... .... [*stuff*] ... ... [*stuff*]
Where the dots ... and *stuff* are just numbers and text
I am trying to ...
0
votes
1answer
65 views
python function to convert camel case to snake case [duplicate]
I managed to cobble together a python function using regular expressions to convert camel to snake case and it works for all my test cases, yet I still have a couple questions.
1) What is each of the ...
0
votes
2answers
46 views
Matching IP with Regex [duplicate]
I am trying to display both the datetime and the IP address of my log file:
Apr 20 07:03:53 123.345.45.123
^ ^ ^ ^
|---datetime--| |-----IP-----|
my code:
datetimeRegex = ...
0
votes
1answer
33 views
printing dates in a log file
I have a log file with a consistent date in my log file.
ex:
date1
date2
...
The date means the number of logs in my log file. I was wondering how can print the dates from the log file using ...
0
votes
2answers
61 views
Regular expressions for finding patterns in Python
I am having a trouble with adjusting regular expression formula to do what I want.
I want to find a pattern:
AxxHxxxAxxHxxxbbbbbAxxHxxxAxxHxxx
But not:
...
0
votes
1answer
35 views
Search and replace string with reverse
I am trying to find words in a string and replace them with themselves in reverse-form.
So, when I have This 17, I want to put out sihT 17.
But I don't know how to reverse the string itself in ...
1
vote
3answers
74 views
Am I misunderstanding Python regex?
So, I am finding that
re.match("[^0-9.]","50-100")
Does not match anything. Can anyone tell me why?
I am convinced that the - should indicate a character range and the ^ should negate the set.
...
0
votes
2answers
44 views
How to write regular expression url for id following slug data?
I am not familiar with regular expression. I need to match the following url with regular expression,
/12 == /id --> Need to match.
/12/what-is-it/ == /id/title/ --> Need to match
/12/what-is-it/12 ...
0
votes
3answers
49 views
Regex for finding jpg
I have a problem when I try to get (https:\\XXXXX.jpg)
I'm using this format: (https:\\.*.jpg) However it doesn't find what I want.
It returns, for example, (https:\\XXXXX.jpg <\div> bla bla ...
0
votes
3answers
30 views
Regex search to extract float from string. Python
import re
sequence = 'i have -0.03 dollars in my hand'
m = re.search('(have )(-\w[.]+)( dollars\w+)',sequence)
print m.group(0)
print m.group(1)
print m.group(2)
Looking for a way to extract text ...
0
votes
2answers
23 views
Python: RE issue with “re.findall()”
string = "RegisterParameter uri wub {"
RegisterName = re.findall("RegisterParameter uri ([^ ]*) {",string)
print 'RegisterName is :',RegisterName
See the above code. Here i want to find register ...
0
votes
1answer
28 views
Pesterchum - Python - Determine word length and capitalize specific character
Im working with python to create a mod for an existing program.
This is like a plugin, where in I am trying to utilize their existing structure to create specialized capitalization mechanics for ...
1
vote
4answers
36 views
How to read access-log hosts with regex?
I have such entries:
e179206120.adsl.alicedsl.de
safecamp-plus-2098.unibw-hamburg.de
p5B30EBFE.dip0.t-ipconnect.de
and I would like to match only the main domain names like
alicedsl.de
...
0
votes
2answers
81 views
Python re.findall() doesn't terminate
Below I have a rather complicated regex that seems to never terminate. It's not a matter of simply taking a long time -- I've waited several minutes for a response with no luck.
Below is a bit of ...
-1
votes
0answers
63 views
1
vote
1answer
56 views
Escaping special characters like ) in regular expressions in Python [duplicate]
I have a dictionary and string like :
d = {'ASAP':'as soon as possible', 'AFAIK': 'as far as I know'}
s = 'I will do this ASAP, AFAIK. Regards, X'
I want to replace the values of dict with the ...
0
votes
2answers
24 views
RegEx phone number doesn't match
I am trying to match following formats:
06142/898-301
+49 6142 898-301
with this
(([+][\d]{2}[ ])|0)([\d]{4}/)([/d]{2,}[.-])+
Debuggex Demo
But after the area code before the / it won't match ...
3
votes
1answer
36 views
Python - PCRE_NOTEMPTY
This is a very basic question, but I am stuck, so any help would be appreciated.
import re
re.split(r'(\d|\W|\_)', ' ab2c d_ef')
['', ' ', '', ' ', 'ab', '2', 'c', ' ', 'd', '_', 'ef']
How do I ...
1
vote
2answers
43 views
Matching and parsing to extract information from complicated string
I have a string with the arguments from a function such as 'arg0, arg1=1, arg2=None' and would like to extract the names and default values for each argument. I have no problems handling simple cases. ...
1
vote
1answer
36 views
cleaner code for validation of string with regular expression
I want to do something like
if re_validate("/\d+/\d+", token):
items.append( token)
instead of this stuff whose meaning is not apparent:
maybe_item = re.findall("/\d+/\d+", token)
if ...
2
votes
1answer
36 views
Python Regular Expression of long complex string
So I am scraping data from a webpage and the received data usually is as followed:
233989 001 0 / 49 T R 4:15 PM - 5:30 PM 205 IST Building 01/13/14 - 05/02/14 Controls View (814) 865-8947 266200 002 ...
0
votes
4answers
69 views
change multiple lines in file python using regex
I have the following text file with repeated block of data. I want to change only the values A, B, C in each block of data and write the updated block of data in a file. How can I do this with a ...
0
votes
3answers
29 views
Returning characters after regex has found match in python
I have a massive text file that I am searching through to find a small amount of data. Say a part of the string looks like this:
'Schedule (1) GEORGE JETSON\n\nPRESIDENT X X 0 O O\n\n(2) BILL ...
0
votes
5answers
72 views
Regular Expression Get Computer Specs from Plain Text
I have a dataset that contains plain text, description about computers and they look similar like below:
I am trying to predict the price based on the text and here is my approach.
Since I know ...
2
votes
2answers
33 views
Regex for matching string with trailing whitespace works in Perl but not Python
I'm trying to find strings that have trailing whitespace, i.e. 'foo ' as opposed to 'foo'.
In Perl, I would use:
$str = 'foo ';
print "Match\n" if ($str =~ /\s+$/) ;
When I try this in Python 2.6, ...
0
votes
1answer
43 views
Split a Python String Using Multiple Delimiters
I have a somewhat complex filename following the pattern s[num][alpha1][alpha2].ext that I'm trying to tokenize. The lexicons from which alpha1 and alpha2 are drawn are contained in two lists.
I ...