Regular expression in Python [closed] - python

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
What is the regular expression that accepts all the words start with Alphabet only and reject all words having any occurrence of less than or more than three consecutive forward slashes (///) [if and only if slashes exist].
Example:
ABC2123_987 is allowed.
AV23DS///KOLJH is allowed.
But, the word FDG56/HJU is not allowed.
Also, FDG56////HJU is not allowed.

This matches any string beginning with an alphabetic, and containing zero slashes or exactly three consecutive slashes.
^[A-Za-z][^/]*(///[^/]*)?$

Try the pattern
re.compile('(\w+(///)?)*')
A better pattern, as seen in the comments for this answer:
re.compile('^[a-zA-Z]\w*?(///)?(\w+(///)?)*$')

Related

Combining two regular expressions with different grouping requirements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have two different repeated character substitution rules I'd like to combine into one regex.
I can do this in python 3.x:
import re
s = r'http://www.google.com/search=ooo-eeee-aa-ii-uuuu'
aiu=re.compile(r'(([aiu])\2{1,})')
eo=re.compile(r'(([eo])\2{2,})')
eo.sub(r'\2',aiu.sub(r'\2',s))
IF there is a major performance gain (this operation will be applied millions of times), is there a single regex expression that achieves what these two achieve (without having to nest calls like I did above).
You can combine the two substitutions with an alternation pattern. The replacement string can be both \1 and \2 together, since one of them will be empty and not affect the output anyway.
aeiou = re.compile(r'([aiu])\1{1,}|([eo])\2{2,}')
aeiou.sub(r'\1\2', s)

How to extract a interrogation sentence from a string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string. For example :
"This is a string.Is this a question?What is the Question? I Dont know what the question is. Can you please list out the question?"
I want to extract the questions from this text using regex
what i tried
re.findall(r'(how|can|what|where|describe|who|when)(.*?)\s*\?',message,re.I|re.M))
But it gives out other things as well and if I gives the questions it separates the (how what which etc) and the rest of the question
For the above example my output is
[('is', ' is a string.Is this a question'), ('What', ' is the Question'), ('what', ' the question is. Can you please list out the question')]
Where as I want the entire question to be together.
It's totally impractical to search for key words when determining whether a sentence is a question. Given your list: how|can|what|where|describe|who|when, I can easily write sentences containing one of those words, which are not questions!
There are many ways you could tackle matching a sentence. For example, taking this as a baseline:
^\s*[A-Za-z,;'"\s]+[.?!]$
We could first alter it to match multiple sentences in the same string:
(^|(?<=[.?!]))\s*[A-Za-z,;'"\s]+[.?!]
This uses a look-behind to ensure that a sentence has just finished (unless we're at the start of the string).
And then adjust it to match only sentences which end with ?:
(^|(?<=[.?!]))\s*[A-Za-z,;'"\s]+\?
Here is an online demo of my regex, on your original string.
To have the entire question together, you should just enclose the whole pattern in parenthesis.
Here is another, simplified version:
\b([A-Z][^.!]*[?])
Thank you for helping me out
the answer was provided by #Fredrik
and can be found here https://regex101.com/r/rT1mQ0/2
\s*([^.?]*(?:how|can|what|where|describe|who|when)[^.?]*?\s*\?)

Insert a word into the middle of a list and print out the modified list [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm doing a Computer Science Lab with Python.
I've tried using some functions like s.insert (hot day, warm morning) but this doesn't help me add the value to the middle of the list. Can someone please help me out?
Thanks
Do this using insert
s.insert(index_to_insert, value)
For example:
s.insert(1, "hello") will insert the string "hello" as the second element of the list (since indexing starts at 0)
The problem is that you have s as a string not a list. You must convert to list then do the insertion as follows:
s = s.split()
s.insert(1, "warm")
s = ' '.join(s)

How to match words containing characters and digits, but not numbers? [closed]

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
I'm trying to create in Python a regular expression that matches words that contains A-Za-z or A-Za-z0-9, but not only 0-9.
For example I want to match fooT, foo23, fo24ooo, fo4o444, but NOT 40.
Is it possible?
One way would be:
r'\w*[A-Za-z]\w*'
\w matches _ as well as 'A-Za-z0-9' - if that's wrong, write out the whole class:
r'[A-Za-z0-9]*[A-Za-z][A-Za-z0-9]*'

Want to make a regular expression that matches the following string [closed]

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
I have to make a regexp that matches this:
ORIGIN atcgt --(a dna sequence varying in length)-- //
But my skills aren't that good
You can use this pattern ^[ATCGU]+$
import re
dnpattern = re.compile("^[ATCGU]+$")
print dnpattern.match("ATC").group()

Categories