Extract a number from a string anytime after a certain character using Python regex [duplicate]
This question already has an answer here:
match string after the last colon
3 answers
I have the following string:
"(rating 1-10, details): 9 is my score"
I want to extract all numbers into a list that occur after :
. The desired output is [9]
Assumptions
-Only one colon in expression
-0 or more values after the colon
-text before the colon may or may not contain numbers
-characters between colon and numbers may or may not be just spaces
I tried using this REGEX expression in Python:
':*(d+)'
My logic here is that the program should find the colon and then find digits an undetermined number of characters later. I am not sure how to handle the number of characters between the colon and the number, which varies. How do I do this?
python regex python-3.x wildcard
marked as duplicate by Wiktor Stribiżew
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 20:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
match string after the last colon
3 answers
I have the following string:
"(rating 1-10, details): 9 is my score"
I want to extract all numbers into a list that occur after :
. The desired output is [9]
Assumptions
-Only one colon in expression
-0 or more values after the colon
-text before the colon may or may not contain numbers
-characters between colon and numbers may or may not be just spaces
I tried using this REGEX expression in Python:
':*(d+)'
My logic here is that the program should find the colon and then find digits an undetermined number of characters later. I am not sure how to handle the number of characters between the colon and the number, which varies. How do I do this?
python regex python-3.x wildcard
marked as duplicate by Wiktor Stribiżew
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 20:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
1
Do you mean like:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?
– The fourth bird
Nov 12 '18 at 20:00
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03
add a comment |
This question already has an answer here:
match string after the last colon
3 answers
I have the following string:
"(rating 1-10, details): 9 is my score"
I want to extract all numbers into a list that occur after :
. The desired output is [9]
Assumptions
-Only one colon in expression
-0 or more values after the colon
-text before the colon may or may not contain numbers
-characters between colon and numbers may or may not be just spaces
I tried using this REGEX expression in Python:
':*(d+)'
My logic here is that the program should find the colon and then find digits an undetermined number of characters later. I am not sure how to handle the number of characters between the colon and the number, which varies. How do I do this?
python regex python-3.x wildcard
This question already has an answer here:
match string after the last colon
3 answers
I have the following string:
"(rating 1-10, details): 9 is my score"
I want to extract all numbers into a list that occur after :
. The desired output is [9]
Assumptions
-Only one colon in expression
-0 or more values after the colon
-text before the colon may or may not contain numbers
-characters between colon and numbers may or may not be just spaces
I tried using this REGEX expression in Python:
':*(d+)'
My logic here is that the program should find the colon and then find digits an undetermined number of characters later. I am not sure how to handle the number of characters between the colon and the number, which varies. How do I do this?
This question already has an answer here:
match string after the last colon
3 answers
python regex python-3.x wildcard
python regex python-3.x wildcard
edited Nov 12 '18 at 20:01
asked Nov 12 '18 at 19:57
Nick Solonko
171217
171217
marked as duplicate by Wiktor Stribiżew
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 20:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Wiktor Stribiżew
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 12 '18 at 20:12
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
1
Do you mean like:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?
– The fourth bird
Nov 12 '18 at 20:00
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03
add a comment |
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
1
Do you mean like:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?
– The fourth bird
Nov 12 '18 at 20:00
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
1
1
Do you mean like
:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?– The fourth bird
Nov 12 '18 at 20:00
Do you mean like
:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?– The fourth bird
Nov 12 '18 at 20:00
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03
add a comment |
1 Answer
1
active
oldest
votes
You can use this regex and find all numbers, that appear after colon in your string.
d+(?!.*:)
Explanation:
d+
--> Matches one or more digits(?!.*:)
--> Negative look aheads to ensures colon does not appear ahead of numbers where we are not interested to capture
Demo
Here is a sample python code,
import re
s = '(rating 1-10, 56 details): 9 is my 22 jj 45 score'
numbers = re.findall('d+(?!.*:)', s)
print(numbers)
This prints following output,
['9', '22', '45']
And does not capture 1, 10 and 56 as they occur before colon.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use this regex and find all numbers, that appear after colon in your string.
d+(?!.*:)
Explanation:
d+
--> Matches one or more digits(?!.*:)
--> Negative look aheads to ensures colon does not appear ahead of numbers where we are not interested to capture
Demo
Here is a sample python code,
import re
s = '(rating 1-10, 56 details): 9 is my 22 jj 45 score'
numbers = re.findall('d+(?!.*:)', s)
print(numbers)
This prints following output,
['9', '22', '45']
And does not capture 1, 10 and 56 as they occur before colon.
add a comment |
You can use this regex and find all numbers, that appear after colon in your string.
d+(?!.*:)
Explanation:
d+
--> Matches one or more digits(?!.*:)
--> Negative look aheads to ensures colon does not appear ahead of numbers where we are not interested to capture
Demo
Here is a sample python code,
import re
s = '(rating 1-10, 56 details): 9 is my 22 jj 45 score'
numbers = re.findall('d+(?!.*:)', s)
print(numbers)
This prints following output,
['9', '22', '45']
And does not capture 1, 10 and 56 as they occur before colon.
add a comment |
You can use this regex and find all numbers, that appear after colon in your string.
d+(?!.*:)
Explanation:
d+
--> Matches one or more digits(?!.*:)
--> Negative look aheads to ensures colon does not appear ahead of numbers where we are not interested to capture
Demo
Here is a sample python code,
import re
s = '(rating 1-10, 56 details): 9 is my 22 jj 45 score'
numbers = re.findall('d+(?!.*:)', s)
print(numbers)
This prints following output,
['9', '22', '45']
And does not capture 1, 10 and 56 as they occur before colon.
You can use this regex and find all numbers, that appear after colon in your string.
d+(?!.*:)
Explanation:
d+
--> Matches one or more digits(?!.*:)
--> Negative look aheads to ensures colon does not appear ahead of numbers where we are not interested to capture
Demo
Here is a sample python code,
import re
s = '(rating 1-10, 56 details): 9 is my 22 jj 45 score'
numbers = re.findall('d+(?!.*:)', s)
print(numbers)
This prints following output,
['9', '22', '45']
And does not capture 1, 10 and 56 as they occur before colon.
edited Nov 12 '18 at 20:18
answered Nov 12 '18 at 20:03
Pushpesh Kumar Rajwanshi
5,3022827
5,3022827
add a comment |
add a comment |
are the characters between : and the number always spaces?
– MegaBluejay
Nov 12 '18 at 19:59
1
Do you mean like
:.*?(d+)
Demo Or match only 1 time a colon and then 1 time the digits?– The fourth bird
Nov 12 '18 at 20:00
@MegaBluejay not necessarily. I updated my question.
– Nick Solonko
Nov 12 '18 at 20:03