Python regexp to get substring contains '/\' - python-3.x

I have string
ss='/users/parun/kk/jdk/bin/\x1b[01;31m\x1b[kjava\x1b[m\x1b[k'
How to get output
'/users/parun/kk/jdk/bin' only from the above
I tried
import re
re.split(r'\/\\')
But not working

A regex search might be the best option here:
ss = '/users/parun/kk/jdk/bin/\x1b[01;31m\x1b[kjava\x1b[m\x1b[k'
path = re.findall(r'^.*/jdk/bin', ss)[0]
print(path) # /users/parun/kk/jdk/bin

Related

How to split a string without touching some chars in python?

So I have this string:
'm(1,2),m(4,3)'
How can I split it to get list that contains only 2 elements:
['m(1,2)', 'm(4,3)']
I can't use str.split function because it will split the whole string and would give me something like this:
['m(1', '2)', 'm(4', '3)']
Can you help me?
You can try regex:
import re
regex = re.compile("m\([0-9],[0-9]\)")
s = "m(1,2),m(4,3)"
regex.findall(s)
should yield:
['m(1,2)', 'm(4,3)']

extract data between single quotes

trying to extract the data between single quotes
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(re.findall(r'()', a))
expecting the oputput : USA-APA HA-WBS-10.152.08.0/24
What is wrong with ? It is just a string ?
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(a)
Output:
% python3 test.py
USA-APA HA-WBS-10.152.08.0/24
You might want to look at this also regarding quotes and strings:
Single and Double Quotes | Python
I am not very familiar with python but with some quick searching around
I've found that this work
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
result = re.findall(r'(.*?)', a)
print("".join(result))
I'm pretty sure there are better ways of solving this but I'm not familiar with the language

Get number from string in Python

I have a string, I have to get digits only from that string.
url = "www.mylocalurl.com/edit/1987"
Now from that string, I need to get 1987 only.
I have been trying this approach,
id = [int(i) for i in url.split() if i.isdigit()]
But I am getting [] list only.
You can use regex and get the digit alone in the list.
import re
url = "www.mylocalurl.com/edit/1987"
digit = re.findall(r'\d+', url)
output:
['1987']
Replace all non-digits with blank (effectively "deleting" them):
import re
num = re.sub('\D', '', url)
See live demo.
You aren't getting anything because by default the .split() method splits a sentence up where there are spaces. Since you are trying to split a hyperlink that has no spaces, it is not splitting anything up. What you can do is called a capture using regex. For example:
import re
url = "www.mylocalurl.com/edit/1987"
regex = r'(\d+)'
numbers = re.search(regex, url)
captured = numbers.groups()[0]
If you do not what what regular expressions are, the code is basically saying. Using the regex string defined as r'(\d+)' which basically means capture any digits, search through the url. Then in the captured we have the first captured group which is 1987.
If you don't want to use this, then you can use your .split() method but this time provide a split using / as the separator. For example `url.split('/').

How to python delete unwanted icon characters

Is there a way to get a string to get rid of icon characters automatically?
input: This is String 💋👌✅this is string✅✍️string✍️✔️
output wish: This is String this is stringstring
replace('💋👌✅', '') is not used because the icon character changes within each string without our prior knowledge of the content
Try this:
import re
def strip_emoji(text):
RE_EMOJI = re.compile(u'([\U00002600-\U000027BF])|([\U0001f300-\U0001f64F])|([\U0001f680-\U0001f6FF])')
return RE_EMOJI.sub(r'', text)
print(strip_emoji('This is String 💋👌✅this is string✅✍️string✍️✔️'))
Consider using the re module in Python to replace characters that you don't want. Something like:
import re
re.sub(r'[^(a-z|A-Z)]', '', my_string)

How to use python to convert a backslash in to forward slash for naming the filepaths in windows OS?

I have a problem in converting all the back slashes into forward slashes using Python.
I tried using the os.sep function as well as the string.replace() function to accomplish my task. It wasn't 100% successful in doing that
import os
pathA = 'V:\Gowtham\2019\Python\DailyStandup.txt'
newpathA = pathA.replace(os.sep,'/')
print(newpathA)
Expected Output:
'V:/Gowtham/2019/Python/DailyStandup.txt'
Actual Output:
'V:/Gowtham\x819/Python/DailyStandup.txt'
I am not able to get why the number 2019 is converted in to x819. Could someone help me on this?
Your issue is already in pathA: if you print it out, you'll see that it already as this \x81 since \201 means a character defined by the octal number 201 which is 81 in hexadecimal (\x81). For more information, you can take a look at the definition of string literals.
The quick solution is to use raw strings (r'V:\....'). But you should take a look at the pathlib module.
Using the raw string leads to the correct answer for me.
import os
pathA = r'V:\Gowtham\2019\Python\DailyStandup.txt'
newpathA = pathA.replace(os.sep,'/')
print(newpathA)
OutPut:
V:/Gowtham/2019/Python/DailyStandup.txt
Try this, Using raw r'your-string' string format.
>>> import os
>>> pathA = r'V:\Gowtham\2019\Python\DailyStandup.txt' # raw string format
>>> newpathA = pathA.replace(os.sep,'/')
Output:
>>> print(newpathA)
V:/Gowtham/2019/Python/DailyStandup.txt

Resources