Regex to extract the date based on particular string [duplicate] - python

This question already has answers here:
Python/Regex - How to extract date from filename using regular expression?
(5 answers)
Closed 2 years ago.
am trying to extract the date if it matches to a particular regex
Ex :
string1 = '10/22/2019 from'
string2 = '12/22/2020 33455SE'
string3 = '7/20/2020 S0023'
Am trying to extract the string 2
Regex used :
r'(\d+[/]\d+[/]\d+[-\s\.]\d+)'
The above used regex is giving me if the string looks like, "10/22/2019 33455" but if there is a alphabet after as shown "33455SE", my code fails.
Any help ?
Tried codes :
r'(\d+[/]\d+[/]\d+[-\s\.]^\d+)' - Tried to use starts with.
Expected output : only string 2 and string 3
12/22/2020
7/20/2020

This works
import re
a = "3443E hello 10/22/2019 33455SE"
number = re.findall(r"[0-9]{2}[/][0-9]{2}[/][0-9]{4}",a)
print(number[0])
Output :
10/22/2019

This should work:
r'(\d+[/]\d+[/]\d+[-\s\.]\d+[A-Z]*)'

\d{1,2}/\d{2}/\d{4}(?=\s\w*\d+)
https://regex101.com/r/gCXHQ6/3

Related

How to extract the first float from a string in python [duplicate]

This question already has answers here:
Extract float/double value
(5 answers)
Closed 11 months ago.
I have a string containing a string and word, I want to extract the first float the string.
myString = 12.5% per month
myFloat= [float(s) for s in re.findall(r'\b\d+\b', myString )][0]
I want to have 12.5 as myFloat.
Thank you
To not change your code completly:
import re
myString = "12.5% 35.6 per month"
myFloat= [float(s) for s in re.findall(r'[0-9]+\.[0-9]+', myString )][0]
All I've changed is the regex expression to r'[0-9]+\.[0-9]+'.
But, as Oliver pointed in his comment, you dont need to use re.findall to get the first occurrence.
You can simply: myFloat= float(re.search(r'[0-9]+\.[0-9]+', myString).group(0))

Python - How to get only the numbers from a string which has got commas and hyphens in between [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 11 months ago.
Consider like I have a string :
stringA = "values-are-10-1,20-2,30-1,40-4,50-3"
I need to get only the strings : desired output :
for stringA: 10-1,20-2,30-1,40-4,50-3
Could you please help me in getting regex for this.
I suggest you use regex module:
import re
stringA = "values-are-10-1,20-2,30-1,40-4,50-3"
re.sub("[^0-9\-\,]", "", stringA).strip("-")
Output
10-1,20-2,30-1,40-4,50-3
You can use this regex to do what you want :
(.[0-9]-[0-9])
Demo
With python code, you can do like this :
import re
regex = r"(.[0-9]-[0-9])"
stringA = "\"values-are-10-1,20-2,30-1,40-4,50-3\""
print([x.group() for x in re.finditer(regex, stringA, re.MULTILINE)])
Output :
['10-1', '20-2', '30-1', '40-4', '50-3']

Extract VAT identification number with Python [duplicate]

This question already has answers here:
What do ^ and $ mean in a regular expression?
(2 answers)
Closed 2 years ago.
I am trying to extract the German VAT number (Umsatzsteuer-Identifikationsnummer) from a text.
string = "I want to get this DE813992525 number."
I know, that the correct regex for this problem is (?xi)^( (DE)?[0-9]{9}|)$.
It works great according to my demo.
What I tried is:
string = "I want to get this DE813992525 number.
match = re.compile(r'(?xi)^( (DE)?[0-9]{9}|)$')
print(match.findall(string))
>>>>>> []
What I would like to get is:
print(match.findall(string))
>>>>> DE813992525
When searching within a string, dont use ^ and $:
import re
string = """I want to get this DE813992525 number.
I want to get this DE813992526 number.
"""
match = re.compile(r'DE[0-9]{9}')
print(match.findall(string))
Out:
['DE813992525', 'DE813992526']

How to find 6 digits in a string with a particular pattern on the first 3 digits in Python? [duplicate]

This question already has answers here:
re.findall behaves weird
(3 answers)
Closed 2 years ago.
I am trying to find a regular expression to return to me the entire 6 digits with the first 3 digits as a pattern/fixed.
Ex:
import re
string_ex = 'docs/data/622999/2013904065003_file.bin'
re.findall(r'622(\d{3})',string_ex)
results in just ['999']
but I want the result to be ['622999']
Thanks!
You should include 622 too within the parenthesis
>>> import re
>>> string_ex = 'docs/data/622999/2013904065003_file.bin'
>>> re.findall(r'(622\d{3})',string_ex)
['622999']
You can use "index" on the string directly.
i = string_ex.index("622")
found = string_ex[i-3:i+2]
https://www.tutorialspoint.com/python/string_index.htm

re.findall only finding half the patterns [duplicate]

This question already has answers here:
Why doesn't [01-12] range work as expected?
(7 answers)
Closed 4 years ago.
I'm using re.findall to parse the year and month from a string, however it is only outputting patterns from half the string. Why is this?
date_string = '2011-1-1_2012-1-3,2015-3-1_2015-3-3'
find_year_and_month = re.findall('[1-2][0-9][0-9][0-9]-[1-12]', date_string)
print(find_year_and_month)
and my output is this:
['2011-1', '2012-1']
This is the current output for those dates but why am I only getting pattern matching for half the string?
[1-12] doesn't do what you think it does. It matches anything in the range 1 to 1, or it matches a 2.
See this question for some replacement regex options, like ([1-9]|1[0-2]): How to represent regex number ranges (e.g. 1 to 12)?
If you want an interactive tool for experimenting with regexes, I personally recommend Regexr.
Adjust your regex pattern as shown below:
import re
date_string = '2011-1-1_2012-1-3,2015-3-1_2015-3-3'
find_year_and_month = re.findall('([1-2][0-9]{3}-(?:1[0-2]|[1-9]))', date_string)
print(find_year_and_month)
The output:
['2011-1', '2012-1', '2015-3', '2015-3']

Categories

Resources