Python Forum
Need help with my code (regular expression)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with my code (regular expression)
#1
Hi there,

I am new to the world of python, sorry for my primitive question!

Here is my code
import re
import collections

txt = “MCV L +67 83.0 - 101.0 -34 fL something”
result = re.findall(r"[-|+]?([0-9]*.[0-9]+|[0-9]+)", txt)
word = re.findall(r"\s(mm|milj./µL|g/dL|%|fL|pg|/µL|something)", txt)

def get_number_of_elements(list):
count = 0
for element in list:
count += 1
return count

count = get_number_of_elements(result)

for i in range (0,count):
print (result[i])

print(“Value is”,result[0])
print (“Range is”, result[1] + " - " + result[2],word[0])

Here is my question →
I was hoping result will give me +67 and -34 but it gives me 67, 34.
Although I have added optional + or - to be recognized, it never takes that into account.
What am I missing ? Please help!
Thanks in advance
Reply
#2
First, please put your code inside python bbcode tags (use the python button above the editor) so that indentation is preserved.

Next, the character class you create with square brackets already matches everything inside. You don't need a pipe symbol (unless you actually want to match a pipe character).

Finally your character class is matching, but it's outside your capturing parenthesis. So the result you get excludes the sign. Just move it inside the parenthesis.

I prefer using the \d for matching digits. So if you're just trying to capture any floating point numbers, I'd probably instead use:

result = re.findall(r"([-+]?\d*\.?\d+)", txt)
Quercus likes this post
Reply
#3
Thanks for help/suggestions.

On little further search, I figured following expression does the trick perfectly.
result = re.findall(r"[-+]?[0-9]*.?[0-9]+", txt)
Reply
#4
Beware the dot. It's unescaped in your last version so it can match other characters (including spaces).

>>> re.findall(r"[-+]?[0-9]*.?[0-9]+", "5.72 +45x843 hi 32")
['5.72', '+45x843', ' 32']
>>> re.findall(r"[-+]?\d*\.?\d+", "5.72 +45x843 hi 32")
['5.72', '+45', '843', '32']
Reply
#5
A helpful document for composing regular expressions is Python 3 reference: re — Regular expression operations.

The special characters are listed and discussed in the section on Regular Expression Syntax. This is useful for composing a tentative regular expression, and for refining it after identifying characters that need to be escaped, such as the dot in the current case.
Reply
#6
(Apr-04-2022, 12:03 AM)bowlofred Wrote: Beware the dot. It's unescaped in your last version so it can match other characters (including spaces).

>>> re.findall(r"[-+]?[0-9]*.?[0-9]+", "5.72 +45x843 hi 32")
['5.72', '+45x843', ' 32']
>>> re.findall(r"[-+]?\d*\.?\d+", "5.72 +45x843 hi 32")
['5.72', '+45', '843', '32']

Great tip .... thank you Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  data validation with specific regular expression shaheen07 0 296 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Is the following code returning a generator expression? quazirfan 8 1,533 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  Regular Expression search to comment lines of code Gman2233 5 1,594 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,602 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regex Expression With Code Query In Pandas eddywinch82 8 2,274 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Regular Expression for matching words xinyulon 1 2,132 Mar-09-2022, 10:34 PM
Last Post: snippsat
  regular expression question Skaperen 4 2,419 Aug-23-2021, 06:01 PM
Last Post: Skaperen
  How can I find all combinations with a regular expression? AlekseyPython 0 1,637 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython
  Python Regular expression, small sample works but not on file Acernz 5 2,861 Jun-09-2021, 08:27 PM
Last Post: bowlofred
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,365 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020