Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm using jupyter notebook to do some simple Regex patterns but it keeps returning none for these two cases and I can’t see why.
I want to search for 3 to 5 digits pattern
digitRegex = re.compile('r(\d){3,5}')
digitRegex.search('123456789')
should return '12345' but it returns none :(
Same problem here, when trying to find 3 consecutive US phone numbers and I want optional: area code and separated by a comma
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d(,)?){3}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-000')
should return the 3 phone numbers but also returns none :(
Thank you...
In your first code, you put the r prefix inside the string, so it won't work. (Such prefix are used for raw strings.)
Working code:
digitRegex = re.compile(r'\d{3,5}')
digitRegex.search('123456789')
In the second sample, the string won't match because it attempts to get three phone numbers at all and the last one ends with three figures instead of four. You need to fix either your regexp or your phone number.
Working sample with valid numbers matching the original regex:
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d(,)?){3}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-0000')
Working sample with a edited regex matching the original numbers:
phoneRegex = re.compile(r'((\d\d\d-)?\d\d\d-\d\d\d\d?(,)?){3,4}')
phoneRegex.search('My numbers are 415-555-1234,555-4242,212-555-0000')
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have some sentences with emoji Unicode, which consist of Unicode patterns like U0001. I need to extract all the string having U0001 into an array. This is the code that i have tried
import re
pattern = re.compile(r"^U0001")
sentence = 'U0001f308 U0001f64b The dark clouds disperse the hail subsides and one neon lit rainbow with a faint second arches across the length of the A u2026'
print(pattern.match(sentence).group()) #this prints U0001 every time but what i want is ['U0001f308']
matches = re.findall(r"^\w+", sentence)
print(matches) # This only prints the first match which is 'U0001f308'
Any way to extract string to an array?. I don't have much experience in regex.
'U0001f30' is not an emoji codepoint! It's a 9-character string beginning with the letter 'U'.
The way to enter unicode codpoints with more than 4 hex characters is \U0001f308. Likewise to enter a 4-hexadecimal character codepoint: \u0001.
But you can't look for codepoints that start with '0001' as if they were regular character strings. It seems to me you are either looking for the 4-hexadecimal character codepoint \u0001 or anything in the range \U00010000 - \U0001FFFF:
import re
sentence = '\U0001f308 \U0001f64b The dark clouds disperse the hail subsides and one neon lit rainbow with a faint second arches across the length of the A \u2026'
matches = re.findall('[\u0001\U00010000-\U0001FFFF]', sentence)
print(matches)
matches -> ['\U0001f308', '\U0001f64b']
If for some reason you really had strings beginning with 'U' and not actual codepoints, then:
matches = re.findall('U0001(?:[0-9a-fA-F]{4})?', sentence)
I have also assumed that emojis can be anywhere in the string and adjacent to any other characters.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
i suspect that this is a simple answer, but i cannot figure out the answer. (I did give it due diligence)
i wrote a simple python program to identify prime numbers. the program is function, but i'm receiving strange results in the output. when i have it write a number with multiple digits, each number is comma separated; for example, 13 is added to the document as 1,3. I would like to have a comma after each full number (13,) and don't want commas within the number (1,3 or 1,301). eventually, i want to have each number on its own row (one of the issue that i ran into in my g1 program is that the row became too long around 50mill ;-)
Any thoughts?
#!/bin/python3
import time
import os
import csv
folderLocation = "c:/notNow/"
primeName = "primeNumbers.csv"
# notPrimeName = "noPrimeNumbers.csv"
primePath=folderLocation + primeName
# notPrimePath=folderLocation + notPrimeName
no=13
os.makedirs(folderLocation)
f = open(primePath, "w")
writer = csv.writer(f)
writer.writerow(str(no))
output: 1,3
writerow expects a sequence of items (e.g list). A string is just seen as a sequence of individual characters, try this instead:
writer.writerow([no])
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am trying to split and capture the 4 words in a sentence and encountered this error: empty separator?
My codes
s1 = input("Enter 3 random strings, separated by commas:") s1 = s1.split(sep = '') print (s1[4])
Thank you for any advices!
try this
s1 = input("Enter 3 random strings, separated by commas:")
s1 = s1.split(sep = ',')
print (s1)
you didnt have the, in sep==""
hope it helped
there's two error in your code
you may change s1=s1.split(), this will split your words.
and at the end of your code, I think you want to print the lest world?
if so, you may be print(s1[2])
because the python count the list from 0, the first one is 0, the third one is 2,
due to your input is 3 words, so the length of the list is 3.you can also print(s1[-1]), it always print out the last one.
For it not to be an empty separator, you should insert a comma inside the two quotes, or else it will technically be empty. In fact, you don't even need the sep because you could just do .split(",")
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Suppose I have a list containing 9 fields, and the 9th field is a string.
Then print(line) will print the entire line, print(line[9]) will return something like:
1/0:.:PASS:90:204,90:201,88:48,39:-204,0,-90:-48,0,-39:14:9,5:5
but print(line[9[0:1]])
will return
File "FileParser.py", line 9, in ?
print(line[9[0:1]])
TypeError: unsubscriptable object
If I assign line[9] to a second var, then I can manipulate it like a string, but this seems like a silly extra step.
Is there a way to index directly into the string while still part of the list?
Thank you
You can index it like this
line[9][0:1]
line[9] will get the actual string and you get the range of characters from that string.
When you do line[9[0:1]] you are trying to get the range of values from the number 9, which is not possible. That is why your code fails.
Since line[9] is a string, then you have to do
line[9][0:1]
Let's say line[9] = "some string". Then you can manipulate it with
line[9][0:1]
which will be equivalent to
"some string"[0:1]
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
the following problem in python please .....
Assuming that s is a string of lower case characters.
how would I write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then my program would print
'Number of times bob occurs is: 2'
I am a completely new to python and appreciate any help
If you didn't want to count overlapping bobs as separate values, this would be easy:
s.count('bob')
But you apparently do. (I had to guess that, based on the fact that your intended output is 2 rather than 1… in the future, it would be better to explain your problem instead of leaving it ambiguous.) As the help says, count returns "the number of non-overlapping occurrences of substring sub…", so that won't do any good.
So, for that, you will have to do it manually. I'll show an example that should have enough to get you started:
for i in range(len(s)):
if s[i:].startswith('bob'):
print('Found a bob')
A slightly smarter way to do this would be to use the find method on strings. You can find details on this in the online docs, or by typing help(str.find) in the interactive console. Notice that find takes a start argument. You should be able to figure out how this would help you; it may take a bit of work to get the details right, but if you get stuck, you can always post a new question asking for specific help.
You can try this way
string = "BOBOBOBOBOABCDE"
search = "BOB"
print len([i for i in range(len(string)) if string.startswith(search, i)])