This question already has answers here:
Backport Python 3.4's regular expression "fullmatch()" to Python 2
(2 answers)
Closed 5 years ago.
Is there a way to match a complete string before python3.4(as they introduced fullmatch() method here.)
For eg. If i have a string '12.345' and If i want to check for float with no exponential and i use the pattern: r'-?(?:\d+())?(?:\.\d+())?'. But this pattern also works for '12.345abc'.
How can i make re to not match second string '12.345abc'?
Thanks!
You might want to use anchors in combination with filter() and lambda():
import re
strings = ['12.345', '12.345abc']
rx = re.compile(r'^\d+(\.\d+)?$')
numbers = list(filter(lambda x: rx.match(x), strings))
print(numbers)
# ['12.345']
This makes sure no rubbish is matched afterwards.
Related
This question already has an answer here:
Python Regular Expressions for a question mark [duplicate]
(1 answer)
Closed 10 months ago.
I have this dataframe:
d = pd.DataFrame({'test':['test?_Apple','test?_Banana', 'test?_limon']})
And I want to remove the test?_ string from test columns values to get this:
d = pd.DataFrame({'test':['Apple','Banana', 'limon']})
I am trying:
d['test'] = d['test'].str.extract(r'test?_(.*)')
but it returns NAN
Can someone help me on this one?
Escape ? because special regex character:
d['test'] = d['test'].str.extract(r'test\?_(.*)')
This question already has answers here:
What is proper way to test if the input is Korean or Chinese using JavaScript?
(2 answers)
Closed 4 years ago.
How do you find Korean letters using regex in JavaScript?
You can use the following regex
const re = /[\u3131-\uD79D]/ugi
This is the code table that I referenced: http://memory.loc.gov/diglib/codetables/9.3.html
Try it yourself:
const re = /[\u3131-\uD79D]/ugi
console.log("abcde".match(re)) // null
console.log("안녕".match(re)) // ["안", "녕"]
This question already has answers here:
How to get all overlapping matches in python regex that may start at the same location in a string?
(2 answers)
Closed 2 years ago.
problem
split a string by all combination where in each sub string starts with a vowels. For example a string like
BANANA need to be split into ANANA, ANAN, ANA, AN, A, ANA, AN, A, A
What I tried
import re
data_k=re.findall(r'(?=([AEIOU].*))','BANANA')
data_2=[s[:i] for s in data_k for i in range(1,len(s)+1)]
data_2
Do we have any faster method to do this , for large string they are giving me memory error, especially the second operation where I split each value in list.
Here is the solution without regular expression (but it only gives numbers of substrings in that string staring with vowel because If we try to get all combination then we will ends up with memory error for large strings.) .
(https://i.stack.imgur.com/aiA3j.jpg)!
This question already has answers here:
python regular expression replacing part of a matched string
(5 answers)
Closed 4 years ago.
Assume two strings, foo.example.com/1.2.3/sdk-foo-bar.min.js and foo.example.com/1.2.3/sdk-foo-bar-dev.min.js.
By default, the first one is used in the HTML code, but depending on a parameter, I need to replace it with the second (i.e. add the -dev).
I have a regex already (foo\.example\.com/1\.2\.3/(sdk-foo-bar).min\.js) that looks for the string and captures the group sdk-foo-bar, but how can I now replace this group with sdk-foo-bar-dev??
inp = 'foo.example.com/1.2.3/sdk-foo-bar.min.js'
m = re.search('(^.*)(.min.js)', inp)
if m:
print ('%s-%s%s' % (m.group(1), 'dev', m.group(2)))
This question already has answers here:
Python Regex Engine - "look-behind requires fixed-width pattern" Error
(3 answers)
Closed 5 years ago.
I have two regex strings in .net
preRegex = "(?<=(^|[^A-Za-z0-9]+))"
postRegex = "(?=([^A-Za-z0-9]+)|$)"
I want their alternative in python. My problem is let say I have a string
s="I am in aeroplane."
What I need is to replace "aeroplane with aeroPlain" so I want to make regex like
newKey = preRegex + aeroplane + postRegex
pattern = re.compile(newKey,re.IGNORECASE)
new regex string look like
(?<=(^|[^A-Za-z0-9]+))aeroplane(?=([^A-Za-z0-9]+)|$)
but it is giving me error "look-behind requires fixed-width pattern".
I am new to python, Help would be appreciated. Thanks
You can use the following regex:
(^|[^A-Za-z0-9]+)aeroplane([^A-Za-z0-9]+|$)
and when you replace, you can call the back reference to the first and second part of your regex to fetch their value.
Replacement string will be something like '\1aeroPlain\2'.
For more information on backref in python:https://docs.python.org/3/howto/regex.html
Good luck!