Extract a number from a string anytime after a certain character using Python regex [duplicate]












1















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?










share|improve this question















marked as duplicate by Wiktor Stribiżew python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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
















1















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?










share|improve this question















marked as duplicate by Wiktor Stribiżew python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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














1












1








1








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 20:01







Nick Solonko

















asked Nov 12 '18 at 19:57









Nick SolonkoNick Solonko

171217




171217




marked as duplicate by Wiktor Stribiżew python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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 python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

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


















  • 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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    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.






    share|improve this answer




























      1














      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.






      share|improve this answer


























        1












        1








        1






        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 '18 at 20:18

























        answered Nov 12 '18 at 20:03









        Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

        5,5422827




        5,5422827















            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values