how to understand re.match in python? [closed] - python

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 7 years ago.
Improve this question
I have following python code:
import re
result = re.match('a.*b', 'aabab')
result.groups() # result is ()
len(result.groups()) # it's 0
result.group(0) # result is 'aabab'
I only know some basic regex, but I can not understand the groups and group. Could someone give some basic explanation about this.
and more, please give some explanation about Pattern and Matcher in python if possible.

Groups are used to capture specific parts of a regular expression. Your regex does not define any groups, therefore the group count is zero.
group(0) is a special case, and it contains the entire match.

Related

Programing in Python [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 4 months ago.
Improve this question
I need help with python. How can I separate combine word into two words. At the first we don't know what kind of words could be? so we must enter a string at the input (v="") and then that word must be separate. For example we have "AMnidcrheaal" and in output will be "Andrea" and "Michal".
According to the output you gave, you want to separate odd and even indices words which can be done as follows
v="AMnidcrheaal"
even=""
odd=""
for i in range(len(v)):
if i%2==0:
even+=v[i]
else:
odd+=v[i]
print(odd)
print(even)

Splitting strings in 80%/20% parts [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 1 year ago.
Improve this question
I have a list of strings and I only want to keep the first 80% of text of each string. So, if a string has for example 100 words, I only want to keep the first 80 words. The split function is not suitable for this problem.
What function can I use, while iterating over the list, to achieve this?
Why isn't it?
sentence = "long string lots of words..."
parts = sentence.split()
newsentence = ' '.join(parts[:len(parts)*4//5])

Find all words that include four consecutive vowels [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Tetravocalic: Find all words that include four consecutive vowels.
def Tetravocalic(i):
return(re.search("/\b.*[aeiou]+.*[aeiou]+.*[aeiou]+.*[aeiou]+{4}/\b",i)))
Tetravocalic("vajjekiohkkugh")
error: multiple repeat at position 42
Please let me know the meaning of the error and the correct code as well.
ex: Tetravocalic("vajjekiohkkugh apple ororaeeg")
Your regular expression is way too complex, you only need this :'[aeiou]{4}'.
Example code:
def has_four_consecutive_vowels(s):
return bool(re.search('[aeiou]{4}', 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*\?)

How to make a this specific Pattern [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having a problem with patterns.
I have string like this:
string1 = "27.86.80.76.83.45.66.71.80.45.76.68.80.45.67.97.108.108.45.84.105.116.45.77.97.114.105.111"
The strings appear in the middle of one file, with different lengths.
For instance I am reading a file line by line and I need to know if the line has this pattern.
Can you guys point me in the right direction?
There's two different ways to go about this:
Build a parser - much work, but very flexible and possibly best performance (depending on implementation)
Use a regular expression. In your case this could be something like (\d{2,3}\.)+\d{2,3} (shortest string matched should be "111.11")

Categories