Getting wrong data with regex
I'm facing an issue here. Python version 3.7.
https://regex101.com/r/WVxEKM/3
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)
python regex python-3.x parsing
add a comment |
I'm facing an issue here. Python version 3.7.
https://regex101.com/r/WVxEKM/3
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)
python regex python-3.x parsing
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
(w{5}-w{3}-d{6}-d{2}, d+)
?
– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Edit the question to include the value ofpart_number
.
– John Gordon
Nov 22 '18 at 0:01
add a comment |
I'm facing an issue here. Python version 3.7.
https://regex101.com/r/WVxEKM/3
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)
python regex python-3.x parsing
I'm facing an issue here. Python version 3.7.
https://regex101.com/r/WVxEKM/3
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)
python regex python-3.x parsing
python regex python-3.x parsing
edited Nov 22 '18 at 3:45


martineau
67.9k1090183
67.9k1090183
asked Nov 21 '18 at 23:43


Javier RamirezJavier Ramirez
384
384
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
(w{5}-w{3}-d{6}-d{2}, d+)
?
– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Edit the question to include the value ofpart_number
.
– John Gordon
Nov 22 '18 at 0:01
add a comment |
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
(w{5}-w{3}-d{6}-d{2}, d+)
?
– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Edit the question to include the value ofpart_number
.
– John Gordon
Nov 22 '18 at 0:01
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
(w{5}-w{3}-d{6}-d{2}, d+)
?– Dev
Nov 21 '18 at 23:58
(w{5}-w{3}-d{6}-d{2}, d+)
?– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Edit the question to include the value of
part_number
.– John Gordon
Nov 22 '18 at 0:01
Edit the question to include the value of
part_number
.– John Gordon
Nov 22 '18 at 0:01
add a comment |
2 Answers
2
active
oldest
votes
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)
1
Personally, I'd use(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.
– Dev
Nov 22 '18 at 0:20
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'
;)
– user8408080
Nov 22 '18 at 0:21
1
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...
– Dev
Nov 22 '18 at 0:28
1
[wd]
=w
, butw
is not equal to[A-Za-z0-9]
, in Python 3,w
matches any Unicode letter, digit,_
and some diacritics.
– Wiktor Stribiżew
Nov 22 '18 at 9:08
add a comment |
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 tuple
s 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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421995%2fgetting-wrong-data-with-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
1
Personally, I'd use(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.
– Dev
Nov 22 '18 at 0:20
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'
;)
– user8408080
Nov 22 '18 at 0:21
1
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...
– Dev
Nov 22 '18 at 0:28
1
[wd]
=w
, butw
is not equal to[A-Za-z0-9]
, in Python 3,w
matches any Unicode letter, digit,_
and some diacritics.
– Wiktor Stribiżew
Nov 22 '18 at 9:08
add a comment |
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)
1
Personally, I'd use(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.
– Dev
Nov 22 '18 at 0:20
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'
;)
– user8408080
Nov 22 '18 at 0:21
1
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...
– Dev
Nov 22 '18 at 0:28
1
[wd]
=w
, butw
is not equal to[A-Za-z0-9]
, in Python 3,w
matches any Unicode letter, digit,_
and some diacritics.
– Wiktor Stribiżew
Nov 22 '18 at 9:08
add a comment |
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)
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)
edited Nov 22 '18 at 9:08
Wiktor Stribiżew
318k16139221
318k16139221
answered Nov 22 '18 at 0:17
user8408080user8408080
1,4811310
1,4811310
1
Personally, I'd use(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.
– Dev
Nov 22 '18 at 0:20
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'
;)
– user8408080
Nov 22 '18 at 0:21
1
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...
– Dev
Nov 22 '18 at 0:28
1
[wd]
=w
, butw
is not equal to[A-Za-z0-9]
, in Python 3,w
matches any Unicode letter, digit,_
and some diacritics.
– Wiktor Stribiżew
Nov 22 '18 at 9:08
add a comment |
1
Personally, I'd use(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.
– Dev
Nov 22 '18 at 0:20
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'
;)
– user8408080
Nov 22 '18 at 0:21
1
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...
– Dev
Nov 22 '18 at 0:28
1
[wd]
=w
, butw
is not equal to[A-Za-z0-9]
, in Python 3,w
matches any Unicode letter, digit,_
and some diacritics.
– Wiktor Stribiżew
Nov 22 '18 at 9:08
1
1
Personally, I'd use
(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.– Dev
Nov 22 '18 at 0:20
Personally, I'd use
(w{5}-w{3}-d{6}-d{2}, d+)
, which is more specific.– Dev
Nov 22 '18 at 0:20
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
'
;)– user8408080
Nov 22 '18 at 0:21
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
'
;)– user8408080
Nov 22 '18 at 0:21
1
1
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...– Dev
Nov 22 '18 at 0:28
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...– Dev
Nov 22 '18 at 0:28
1
1
[wd]
= w
, but w
is not equal to [A-Za-z0-9]
, in Python 3, w
matches any Unicode letter, digit, _
and some diacritics.– Wiktor Stribiżew
Nov 22 '18 at 9:08
[wd]
= w
, but w
is not equal to [A-Za-z0-9]
, in Python 3, w
matches any Unicode letter, digit, _
and some diacritics.– Wiktor Stribiżew
Nov 22 '18 at 9:08
add a comment |
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 tuple
s 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
add a comment |
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 tuple
s 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
add a comment |
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 tuple
s 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
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 tuple
s 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
edited Nov 22 '18 at 3:34
answered Nov 22 '18 at 0:42


martineaumartineau
67.9k1090183
67.9k1090183
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421995%2fgetting-wrong-data-with-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I have read that group(0) returns complete match, so what am I doing wrong?
– Javier Ramirez
Nov 21 '18 at 23:45
Please copy enough of your input into the question to locally reproduce your results.
– usr2564301
Nov 21 '18 at 23:46
(w{5}-w{3}-d{6}-d{2}, d+)
?– Dev
Nov 21 '18 at 23:58
Your character class isn't matching the single quotes, by the way.
– Dev
Nov 21 '18 at 23:58
Edit the question to include the value of
part_number
.– John Gordon
Nov 22 '18 at 0:01