I have a string like this:
"8 R-301 288/313 0.24 TT-2079 347.48
"
Now I want to extract 288/313 and 0.24 using regex So I wrote this:
r42=re.findall(r"8 +R-301.*",pdf[i])
if len(r42[0].split())>3:
print(r42[0].split())
logbook['R-301 Temp']=r42[0].split()[2]
logbook['R-301 P']=r42[0].split()[3]
So in an ideal case number comes in 2nd and 3rd index and I can get it.
But the problem I'm facing is that sometimes 288/313 has spaces like this 288 / 313, also the second number 0.24 can have a slash too so it can be like 0.24/0.25 or 0.24 /0.25. So the above regex doesn't work.
What would be an ideal regex for these decimal slash numbers with random spaces?
Note: The string can have multiple spaces between characters.
Edit:
Sorry I forgot one detail here:
numbers can have - like in the above example it can be -/313 instead of 288/313 or - instead of 0.24 or 0.24/- or -/0.24.
Something like this:
"8 R-301 288/- - TT-2079 347.48
"
I want to target those cases as well.
\d* - 0 or more digits (the decimal part); $ - End of the line.
The regex [0-9] matches single-digit numbers 0 to 9. [1-9][0-9] matches double-digit numbers 10 to 99. That's the easy part. Matching the three-digit numbers is a little more complicated, since we need to exclude numbers 256 through 999.
[0-9]+|[0-9]+). This regular expression matches an optional sign, that is either followed by zero or more digits followed by a dot and one or more digits (a floating point number with optional integer part), or that is followed by one or more digits (an integer).
To check if a string contains at least one number using regex, you can use the \d regular expression character class in JavaScript. The \d character class is the simplest way to match numbers.
With your shown samples, could you please try following regex.
Let's say following are the values:
var="""8 R-301 288/313 0.24/0.25 TT-2079 347.48
8 R-301 288 / 313 0.24/0.25 TT-2079 347.48
8 R-301 288 / 313 0.24 / 0.25 TT-2079 347.48
8 R-301 - / 313 -/ 0.25 TT-2079 347.48
8 R-301 288 /313 -/ 0.25 TT-2079 347.48
8 R-301 313 / - -/ 0.25 TT-2079 347.48
8 R-301 288 / 313 0.24/ 0.25 TT-2079 347.48
8 R-301 123/313 -/12123 TT-2079 347.48
8 R-301 123.12/31.23 -/12123 TT-2079 347.48
8 R-301 123/313 -/- TT-2079 347.48
8 R-301 123 12123 TT-2079 347.48
8 R-301 -/123 -/- TT-2079 347.48"""
Now following is the code:
import re
val = re.findall(r'^\d+\s+R-\d+\s+(.*?)T.*',var,re.M)
for i in val:
re.findall(r'((?:\d+(?:\.\d+)?|-)(?:(?:\s+)?\/(?:\s+)?(?:\d+(?:\.\d+)?|-))?)',i,re.M)
Output will be as follows:
['288/313', '0.24/0.25']
['288 / 313', '0.24/0.25']
['288 / 313', '0.24 / 0.25']
['- / 313', '-/ 0.25']
['288 /313', '-/ 0.25']
['313 / -', '-/ 0.25']
['288 / 313', '0.24/ 0.25']
['123/313', '-/12123']
['123.12/31.23', '-/12123']
['123/313', '-/-']
['123', '12123']
['-/123', '-/-']
Explanation: Adding detailed explanation for above regex:
( ##Creating 1st capturing group here.
(?:\d+(?:\.\d+)?|-) ##In a non-capturing group matching digits with optional dot digits OR followed by - here.
(?:(?:\s+)?\/ ##In a non-capturing group with optional spaces with / here.
(?:\s+)?(?:\d+(?:\.\d+)?|-))? ##Matching optional spaces with digits and optional digits OR with optional - here.
) ##Closing 1st capturing group here.
Try:
import re
tests = [
"8 R-301 288/313 0.24 TT-2079 347.48",
"8 R-301 288 / 313 0.24 TT-2079 347.48",
"8 R-301 288/313 0.24/0.25 TT-2079 347.48",
"8 R-301 288/313 0.24/ 0.25 TT-2079 347.48",
"8 R-301 288 / 313 0.24 / 0.25 TT-2079 347.48",
"8 R-301 288/- - TT-2079 347.48",
"8 R-301 288/- 0.1 /- TT-2079 347.48",
"8 R-301 -/233 - TT-2079 347.48",
"8 R-301 313 -/12123 TT-2079 347.48",
]
r = re.compile(
r"\s+((?:[\d\.-]+\s*/\s*[\d\.-]+)|[\d\.-]+)\s+([\d\.-]+(?:\s*/\s*[\d\.-]+)?)"
)
for test in tests:
m = r.search(test)
if m:
m = m.groups()
number1 = m[0].replace(" ", "")
number2 = m[1].replace(" ", "")
print(number1, number2)
Prints:
288/313 0.24
288/313 0.24
288/313 0.24/0.25
288/313 0.24/0.25
288/313 0.24/0.25
288/- -
288/- 0.1/-
-/233 -
313 -/12123
EDIT: Updated regex to accept -
EDIT2: Updated regex to accept first value without slash /
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With