1

I'm facing an issue here. Python version 3.7.

https://regex101.com/r/WVxEKM/3

screenshot of regex101 web page

As you can see on regex site, my regex is working great, however, when I try to read the strings with python, I only get the first part, meaning, no values after comma.

Here's my code:

part_number = str(row)
partn = re.search(r"([a-zA-Z0-9 ,-]+)", part_number)
print(partn.group(0))

This is what partn.group(0) is printing:

FMC2H-OHC-100018-00

I need to get the string as regex, with comma and value:

FMC2H-OHC-100018-00, 2

Is it my regex wrong?. What is happening with commas and values?

ROW Values Here are the row values converted to string, the data retrieve from my db also include parentheses and quotes:

('FMC2H-OHC-100018-00', 2)
('FMC2H-OHC-100027-00', 0)
5
  • I have read that group(0) returns complete match, so what am I doing wrong? Commented Nov 21, 2018 at 23:45
  • Please copy enough of your input into the question to locally reproduce your results. Commented Nov 21, 2018 at 23:46
  • \(\w{5}-\w{3}-\d{6}-\d{2}, \d+\)? Commented Nov 21, 2018 at 23:58
  • Your character class isn't matching the single quotes, by the way. Commented Nov 21, 2018 at 23:58
  • Edit the question to include the value of part_number. Commented Nov 22, 2018 at 0:01

2 Answers 2

1

I don't think the you need to convert the row values to string and then try to parse the result with a regex. The clue was when you said in your update that "Here are the row values converted to string" implying that they're in some other format initially—because the result looks they're actually tuples of two values, a string and an integer.

If that's correct, then you can avoid converting them to strings and then trying to parse it with a regex, because you can get the string you want simply by using the relatively simple built-in string formatting capabilities Python has to do it.

Here's what I mean:

# Raw row data retrieved from database.
rows = [('FMC2H-OHC-100018-00', 2),
        ('FMC2H-OHC-100027-00', 0),
        ('FMC2H-OHC-100033-00', 0),
        ('FMC2H-OHC-100032-00', 20),
        ('FMC2H-OHC-100017-00', 16)]

for row in rows:
    result = '{}, {}'.format(*row)  # Convert data in row to a formatted string.
    print(result)

Output:

FMC2H-OHC-100018-00, 2
FMC2H-OHC-100027-00, 0
FMC2H-OHC-100033-00, 0
FMC2H-OHC-100032-00, 20
FMC2H-OHC-100017-00, 16
Sign up to request clarification or add additional context in comments.

Comments

0

Your problem is that you didn't include the ' in your character group. So this regex matches for example FMC2H-OHC-100018-00 and , 2, but not both together. Also re.search stops searching after it finds the first match. So if you only want the first match, go with:

re.search(r"([\w ',-]+)", part_number)

Where I changed A-Za-z0-9 to \w, because it's shorter and more readable. If you want a list that matches all elements, go with:

re.findall(r"([\w ',-]+)", part_number)

4 Comments

Personally, I'd use \(\w{5}-\w{3}-\d{6}-\d{2}, \d+\), which is more specific.
This is also what I would do, but I wanted to change OP's regex as little as possible, because he still might have a reason for this. Good addition, though! Also you still missed the ' ;)
Oh, sorry, I just copied that from my comment which for some reason didn't update in this tab when I corrected it. The regex I have open in regex101 right now is '(\w{5}-\w{3}-\d{6}-\d{2})', (\d+), SO's live update functionality just sucks. Guess I didn't look it over for the 32nd time after copying it over...
[\w\d] = \w, but \w is not equal to [A-Za-z0-9], in Python 3, \w matches any Unicode letter, digit, _ and some diacritics.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.