Parsing a URL using regular expression [duplicate] - python

This question already has answers here:
What's the u prefix in a Python string?
(5 answers)
Closed 6 years ago.
I am trying to parse the 'Meghan' part from the line:
link = http://python-data.dr-chuck.net/known_by_Meghan.html
...with the following regex:
print re.findall('by_(\S+).html$',link)
I am getting the output:
[u'Meghan']
Why I am getting the 'u'?

It means unicode. Depending on what you'll do with it, you can ignore it for the most part, of you can convert it to ascii by doing .encode('ascii')

Related

String replace() vs string translate() method [duplicate]

This question already has answers here:
How to replace two things at once in a string?
(6 answers)
using .replace to replace more than one character in python [duplicate]
(4 answers)
Closed 2 years ago.
I have a question regarding my code below:
Input: A DNA string Pattern (ex: 'AAAACCCGGT')
Output: The complementary string (ex: 'TTTTGGGCCA')
def Complement(Pattern):
comPattern=Pattern.translate(str.maketrans({'A':'T','T':'A','G':'C','C':'G'}))
return comPattern
I tried using str.replace() method multiple times for above problem, but it did not work. Any idea why?

how can I solve keyerror when formatting string in pythong [duplicate]

This question already has answers here:
Python format throws KeyError
(1 answer)
How do I escape curly-brace ({}) characters in a string while using .format (or an f-string)?
(23 answers)
Closed 3 years ago.
I just want the following outcome.
But I get KeyError: '"msg_body"'.
input:
text="text"
uid="uid"
input = '{"msg_body":{input_text}, "user_id":{input_uid}}'.format(input_text=text, input_uid=test)
wanted output:
'{"msg_body":"text", "user_id":"uid"}'
Single or double quotations must be exactly how it is above.
Thanks

Python print ascii character in string instead of value [duplicate]

This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 3 years ago.
I have a string "hello[ World]" and I want to convert it to "hello[World]"
I tried something like this:
a.encode("utf-8").decode("ascii")
I got back same string as input.
Try this:
import html
html.unescape("LASIX [FUROSEMIDE]")
This produces:
'LASIX [FUROSEMIDE]'

How to add an integer into this Regex in Python without hard coding the integer? [duplicate]

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 5 years ago.
I want to eventually be able to increment an integer within my Regex, but the braces are preventing me from doing so.
So far I have:
start = 6
m = re.search(r"(.{{n},}).*?\1".format(n=start), s)
return m.group(1)
However, I get `ValueError: Single '}' encountered in format string
I am using Python 2.7.
What about using a different method of string formatting:
m = re.search(r"({.%s,}).*?\1" % start, s)

re.match in python to match pattern with string [duplicate]

This question already has answers here:
What is the difference between re.search and re.match?
(9 answers)
Closed 7 years ago.
I am trying to match string with mypattern, somehow I do not get correct result. Can you please point where am I wrong?
import re
mypattern = '_U_[R|S]_data.csv'
string = 'X003_U_R_data.csv'
re.match(mypattern, string)
I like to compile the regex statement first. Then I do whatever kind of matching/searching I would like.
mypattern = re.compile(ur'_U_[R|S]_data.csv')
Then
re.search(mypattern, string)
Here's a great website for regex creation- https://regex101.com/#python

Categories