Regex string insertion [duplicate] - python

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 6 years ago.
Simple, simple question, hope you can help me:
How do I add a string to a regex?
Say:
d = '\d\d\d'
mo = re.compile(r #d goes here)
Pasting it, separating it with a comma, or with a plus gives me errors.
Normally, as you know, it would be re.compile(r'\d\d\d')

Is this what you are looking for?
d = r"\d\d\d"
re.compile(d)

Maybe more intuitive:
d = r"\d{3}"
# match a digit exactly three times, consecutively
re.compile(d)

Related

String Replace in python by index wise or by one direction from start to end [duplicate]

This question already has answers here:
Index-wise Replacement of String Data in Python
(1 answer)
String replace in Python, in sequence, by index [duplicate]
(1 answer)
Closed 1 year ago.
I'm a newbie who just Started learning Python from YouTube. I am trying to make a program to replace old string Numbers with new string Numbers and facing problems while replacing numbers. Want to replace index-wise (What is its technical term (I don't know)). It can go in one direction or index-wise.
my string is = (001001001001001001001001001001001001001101100100110110011011001101011010011010110011011)
and I want to replace 101 with 01, 1101 with 11, 1001 with 011, and 11001 with 111,
so my replaced string/output string will be like this..
(00011000110001100011000110001100110110011011010110101100110111011)
As per python's normal string replace method it Cant work Anyone can help my
string = "001001001001001001001001001001001001001101100100110110011011001101011010011010110011011"
string = string.replace('101', '01').replace('1101', '11').replace('1001', '011').replace('11001', '111')
fin.close()
fin = open("2x.txt", "wt")
fin.write(string)
fin.close()
(00011000110001100011000110001100110110011011010110101100110111011)
In general python you can't "edit" strings, you need to create new ones. E.g:
my_old_string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
# use .replace()
my_new_string = my_old_string.replace('010', '0')
You could achieve the same thing with a single variable aswell:
string = '01010110110111011110111101111011110101101101101011011011010101010101010101011101110101110111101'
string = string.replace('010', '0')
string = string.replace('1101', '11')
# continue with this as often as you want
I am not sure, if your "doing all in one line" syntax is valid

How to strip a string with a "." properly in Python? [duplicate]

This question already has answers here:
Python strip unexpected behavior
(2 answers)
Closed 4 years ago.
I would like to strip the ending from a string, as in following:
da = "abc.com"
print(da.strip(".com"))
My expected outcome is abc. However, ab is returned instead.
Why is that and how to solve it?
this should work: simply split string by . and discard the last piece.
print(da.split('.')[:-1])
Using Regex.
import re
da = "abc.com"
print(re.sub("\.com", "", da))
or
print(da.replace(".com", ""))
Output:
abc

Splitting two concatenated terms in python [duplicate]

This question already has answers here:
Split a string at uppercase letters
(22 answers)
Closed 6 years ago.
In general I have a string say
temp = "ProgramFields"
Now I want to split strings like these into two terms(I can identify tow strings based on uppercase character)
term1 = "Program"
term2 = "Field"
How to achieve this in python?
I tried regular expression and splitting terms but nothing gave me the result that I expected
Python code -
re.split("[A-Z][a-z]*","ProgramField")
Any suggestions?
You have to include groups:
re.split('([A-Z][a-z]*)', 'ProgramField)

Find letters in string without using for loop [duplicate]

This question already has answers here:
Extracting only characters from a string in Python
(7 answers)
How do you filter a string to only contain letters?
(6 answers)
Closed 6 years ago.
I was trying to figure out how to list just the letters in a string and ignore the numbers or any other characters. I figured out how to do it using the for loop, but I couldn't find out how to do it without using the for loop.
This is how I used the for loop:
>>> a = "Today is April 1, 2016"
for i in a:
if i.isalpha():
list(i)
Any help will be appreciated!
You can use filter for this:
>>> ''.join(filter(str.isalpha, a))
'TodayisApril'
list(set([x for x in a if x.isalpha()]))
this should do it :)

Get the last 4 characters of a string [duplicate]

This question already has answers here:
How do I get a substring of a string in Python? [duplicate]
(16 answers)
Closed 7 years ago.
I have the following string: "aaaabbbb"
How can I get the last four characters and store them in a string using Python?
Like this:
>>> mystr = "abcdefghijkl"
>>> mystr[-4:]
'ijkl'
This slices the string's last 4 characters. The -4 starts the range from the string's end. A modified expression with [:-4] removes the same 4 characters from the end of the string:
>>> mystr[:-4]
'abcdefgh'
For more information on slicing see this Stack Overflow answer.
str = "aaaaabbbb"
newstr = str[-4:]
See : http://codepad.org/S3zjnKoD

Categories