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
Related
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
I have a list output:
['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
and I want to clean it up in order to output:
[40RAAC34,40RAAC33]
If you have a string:
'hello (world)'
and want the text between the brackets, you can either use a regex:
import re
re.findall('\((.*?)\)', s)[0]
#'world'
or, if you are sure that there is only one set of brackets (i.e. no leading ) chars) then you can just use slicing:
s[s.index('(')+1:s.index(')')]
#'world'
So then you just need to throw this into a list-comprehension or similar.
l = ['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
[s[s.index('(')+1:s.index(')')] for s in l]
#['40RAAC34', '40RAAC33']
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Suppose, I want to define a two-line (or a multi-line) string.
I can do this in two ways:
Using escape sequence for the newline character.\n
Ex: "This is the first sentence. \n This is the second sentence."
Using triple-quoted strings.
Ex: """ This is the first sentence.
This is the second sentence."""
Which is the more efficient or conventional ? Why ?
I'm tempted to say it doesn't matter since each one still scans inside for escaped characters while parsing the text.
>>> print "a\n\tb"
a
b
>>> print """a\n\tb"""
a
b
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
I am not sure how I can extract the variables or groups I created in my regular expression. Specifically datetime and IP. I have read other postings and the documentation but I am getting a bit confused. I was wondering if someone could generate an example for me to follow. What I would like to do is to be able to extract datetime and IP for later use. Perhaps stored in a variable to be called on later
sample log:
log = 'Oct 7 13:24:36 192.168.10.2 2013: 10:07-13:24:35 httpproxy[15359]: id="0001"
httpproxy515139 = re.compile(r'(?P<datetime>\w\w\w\s+\d+\s+\d\d:\d\d:\d\d)\s+(?P<IP>d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).*')
This sample should help you:
>>> import re
>>> sample = 'this is a sample text'
>>> third_word = re.compile(r'\S+ \S+ (?P<word>\S+) .*')
>>> ms = third_word.match(sample)
>>> ms.groupdict()
{'word': 'a'}
You need to access the groupdict() method of the returned match object.
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
How can I replace all the " with the raw string \" in a string such as "Hello", said he. in Python?
s = '"Hello", said he.'
print s.replace('"', r'\"')
# output
\"Hello\", said he.
It helps to use the r'' notation to indicate that the string should be raw and not interpreted. Helps with backslashes.
Use replace().
>>> print '"Hello"'.replace('"', '\\"')
\"Hello\"