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 years ago.
Improve this question
What does .start() do in the following script?
import re
str2 = re.search("\((\w+)\)", str1)
return str1[:str2.start()].strip()
If you are more of a reader, the documentation of match.start() would tell you what it does.
If you are more of an experimenter, open an interactive python console, and input the following (feel free to change the input data, after all you are an experimenter):
>>> import re
>>> str1 = 'Hello (python) world'
>>> str2 = re.search("\((\w+)\)", str1)
>>> str2.start()
6
>>> str1[:6]
'Hello '
>>>
Short explanation: it tells you the index of the starting position of the match.
Hope this answer will teach you something more than just what does match.start() do ;-)
From the Python documentation for the start method
https://docs.python.org/3/library/re.html
It returns the index of the substring that matched.
So, str2.start() is where the regex was matched in str1.
Think of that return as saying,
Returning everything in str1 up to where the regex was matched, and strip whitespace.
Related
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 2 years ago.
Improve this question
I want to remove links in the format Reddit uses
comment = "Hello this is my [website](https://www.google.com)"
no_links = RemoveLinks(comment)
# no_links == "Hello this is my website"
I found a similar question about the same thing, but I don't know how to translate it to python.
I am not that familiar with regex so I would appreciate it if you explained what's happening.
You could do the following:
import re
pattern = re.compile('\[(.*?)\]\(.*?\)')
comment = "Hello this is my [website](https://www.google.com)"
print(pattern.sub(r'\1', comment))
The line:
pattern = re.compile('\[(.*?)\]\(.*?\)')
creates a regex pattern that will search for anything surrounded by square brackets, followed by anything surrounded by parenthesis, the '?' indicates that they should match as little text as possible (non-greedy).
The function sub(r'\1', comment) replaces a match by the first capturing group in this case the text inside the brackets.
For more information about regex I suggest you read this.
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 5 years ago.
Improve this question
For example:
string = "abcdefghi"
separated = "abc" + x + "ghi"
x = ???
I want to find x, using any string.
x=re.search('(?<=abc).*(?=ghi)','abcdefghi').group(0)
print(x)
output
def
Explanation
Regex
(?<=abc) #Positive look behind. Start match after abc
.* #Collect everything that matches the look behind and look ahead conditions
(?=ghi) #Positive look ahead. Match only chars that come before ghi
re.search documentation here.
A Match Object is returned by re.search. A group(0) call on it would return the full match. Detail on Match Object can be found here.
Note:
The regex is aggressive so would match/return defghixyz in abcdefghixyzghi.
See demo here.
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 6 years ago.
Improve this question
im trying to do a kind of basic AI for a school project using python's re, and i wanted to ask is there a way to determine if given pattern exists in string, like
string = raw_input()
"""I type in 'Hey can you check the weather?' """
and if it does find word 'weather' it returns true or false.
I would then use it to run through series of if statements, to chceck what the user wants to do(like weather, date, time, and other things).
Also, i would be very happy to hear from you guys if u had better idea of solving this problem.
You do not need regex for such simple checks. Simply use in to check for given word in your sentence as:
>>> my_string = 'Hey can you check the weather?'
>>> 'weather' in my_string
True
So, in order to check from list of words, you may use any() as:
>>> words_to_check = ['hello', 'world', 'weather']
>>> any(word in my_string for word in words_to_check)
True
As mentioned by #DYZ, in order to do case in-sensitive match, you need to make a check on the lowercased string as:
# Converts string to lowercase v
>>> any(word in my_string.lower() for word in words_to_check)
True
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 6 years ago.
Improve this question
How do I match the following pattern using re?
2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490
I used python's split() function to separate the attributes out but as the data is huge, the process is getting killed due to memory errors.
If you put the long version of string it would be better.
So how can you make it ? That is the answer:
import re
str = "2016-02-13 02:00:00.0,3525,http://www.heatherllindsey.com/2016/02/my-husband-left-his-9-5-job-for-good-it.html,158,0,2584490"
pattern = re.compile("(.*?),", re.DOTALL) #we use re.DOTALL to continue splitting after endlines.
result = pattern.findall(str) #we can't find the last statement (2584490) because of the pattern so we will apply second process
pattern = re.compile("(.*?)", re.DOTALL)
str2 = str[-50:-1]+str[-1] #we take last partition of string to find out last statement by using split() method
result.append(str2.split(",")[-1])
print result
It works...
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 8 years ago.
Improve this question
Stackoverflow is best
I want to make:
"""Stackoverflow is best"""
How can I insert special character with regex or is there any other way to do that?
I think you want something like this,
>>> foo = 'Stackoverflow is best '
>>> import re
>>> foo = re.sub(r'^\s*|\s*$', r'"""', foo)
>>> foo
'"""Stackoverflow is best"""'
Use single quote:
foo = '"""Stackoverflow is best"""'
Or you can use the escape character:
foo = "\"\"\"Stackoverflow is best \"\"\""
You don't need regex to create foo.
Try this
str1 = '"""Stackoverflow is best"""' # you can interchange the double and single quotes in python
print str1