Regex for number after a Parenthesis ( [duplicate] - python

This question already has answers here:
Python - Regular expressions get numbers between parenthesis
(2 answers)
Closed 2 years ago.
I have a column with values like this:
4 (3 in force)
44 (39 in force)
I was able to use this to get a new column for the first number.
df['new'] = df['column'].str.extract('(\d+)')
How can I get a new column for the second number? (3,39, etc.)

One way that specifically answers you question would be to use a lookbehind regular expression, that basically says "the first number after another number, a space and a parenthesis":
df['new'] = df['column'].str.extract('(?<=\d+\s\()\d+')
But if you're extracting multiple parts from a single string, you might consider combining the two and using groups in the regex to access the parts you want.

You could just take the row, convert it into a string, split it, and access the needed numbers, for example:
row = '4 (3 in force)'
row.split(' ') # This returns ['4', '(3', 'in', 'force)']
row.split(' ')[1] # This returns '(3'
row.split(' ')[1][1:] # And this returns all numbers after the bracket, so '3'

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

What's the code if we want to trim the string [duplicate]

This question already has answers here:
How do I remove a substring from the end of a string?
(23 answers)
Closed 2 years ago.
I am kind of noob in python and struck in middle of code. I want to trim my string.
For example- my string is "bangalore store 1321" and i want to trim it to "banglore"
Looks like you want to keep the first word (which is not "trimming" though). So you do two things
break the string into a list of words (where "word" is something separated by spaces)
take the first element of that list
words = mystring.split(' ')
result = words[0]
For a slicing answer:
def sub_string(str, start,end):
return str[start:end]
You can also use split, by definition, this splits by spaces, if any other delimiter needed, you can identity it inside the split arguments split(',')
def split_string(str):
return str.split()
This function will return an array of strings. Choose whichever you want from this array
str="bangalore store 1321"
print(str.split(' ')[0])
Output
bangalore
You can use str's partition method to avoid creating a list like str.split
>>> first_word, _, _ = s.partition(' ') # _ is a convention for a throwaway variable
>>> print(first_word)
bangalore
str.partition takes one argument - the separator - and returns the parts of the string before and after the first occurrence of the separator.

python regex - extract value from string [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I would like to know how can I get from a string and using reg expressions all values until the comma starting from the end. See below example, I would like to get the value "CA 0.810" into a variable:
prue ="VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"
So far, I have the below code:
test = re.findall('([0-9]+)$',prue)
print test
However, I only get below output:
['810']
Could you please advise how can I get "CA 0.810" into the test variable?
You can do this using the split method. From the docs, it will:
Return a list of the words in the string, using sep as the delimiter string.
So if you can take your string:
prue = "VA=-0.850,0.800;CA=-0.863,0.800;SP=-0.860,0.810;MO=-0.860,0.810;SUN=MO -0.850,CA 0.810"
you can do :
prue.split(",")
which will return a list of the strings split by the commas:
['VA=-0.850', '0.800;CA=-0.863', '0.800;SP=-0.860', '0.810;MO=-0.860', '0.810;SUN=MO -0.850', 'CA 0.810']
So if you just want the last item ('CA 0.8101') into a variable named test, you can just take the last element from the list by indexing with -1:
test = prue.split(",")[-1]
test is now: 'CA 0.810'
Hope this helps!

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)

How to extract 4 characters after '-' in a hostname [duplicate]

This question already has answers here:
How to get a string after a specific substring?
(9 answers)
Closed 7 years ago.
I have hostname of format xxxxxxxx-abcdxxxxx the x is not a set number so can't use print text[10:14] because I don't have a set location, the only pattern is 4 chars after -.
Assuming your first string is
s = "xxxxxxxx-abcdxxxxxxxxx"
you just do:
s.split("-",1)[1][:4]
which splits s into two strings in an array, ['xxxxxxxx','abcdxxxxxxxxx'] and you get the result by taking the splicing of the 2nd array from index 0 to 4.
abcd
Option 1
Get the index of the dash and select from +1 to +5:
a = 'xxxxxxx-abcdxxxxxxx'
i = a.index('-')
print(i[i+1:i+5])
Option 2
Use the split function and then get the first 4 values of the second element.
a = 'xxxxxxx-abcdxxxxxx'
print(a.split('-')[1][:4])
To see if a string is alphabetic, simply call the isalpha function:
str.isalpha()
It will return true or false based on result.

Categories