Using RegEx to find number followed by dot





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















I am trying to find the index of the reference in a list of references. Let me illustrate:



This is a list of references I scraped off a website:



ref = "<p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"


I thought I could get the index of reference (i.e. "1." and "2.") by using this:



result = list(map(int, [e for e in re.split("[^0-9]", ref) if e != '']))


But I'm getting all numbers: [1, 2003, 729537528, 2, 2019]



How do I only get the list of reference index, i.e. [1, 2] One way I guess is to find numbers followed by a dot, but I don't know how.










share|improve this question




















  • 2





    Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

    – Wiktor Stribiżew
    Nov 23 '18 at 20:26


















1















I am trying to find the index of the reference in a list of references. Let me illustrate:



This is a list of references I scraped off a website:



ref = "<p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"


I thought I could get the index of reference (i.e. "1." and "2.") by using this:



result = list(map(int, [e for e in re.split("[^0-9]", ref) if e != '']))


But I'm getting all numbers: [1, 2003, 729537528, 2, 2019]



How do I only get the list of reference index, i.e. [1, 2] One way I guess is to find numbers followed by a dot, but I don't know how.










share|improve this question




















  • 2





    Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

    – Wiktor Stribiżew
    Nov 23 '18 at 20:26














1












1








1








I am trying to find the index of the reference in a list of references. Let me illustrate:



This is a list of references I scraped off a website:



ref = "<p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"


I thought I could get the index of reference (i.e. "1." and "2.") by using this:



result = list(map(int, [e for e in re.split("[^0-9]", ref) if e != '']))


But I'm getting all numbers: [1, 2003, 729537528, 2, 2019]



How do I only get the list of reference index, i.e. [1, 2] One way I guess is to find numbers followed by a dot, but I don't know how.










share|improve this question
















I am trying to find the index of the reference in a list of references. Let me illustrate:



This is a list of references I scraped off a website:



ref = "<p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"


I thought I could get the index of reference (i.e. "1." and "2.") by using this:



result = list(map(int, [e for e in re.split("[^0-9]", ref) if e != '']))


But I'm getting all numbers: [1, 2003, 729537528, 2, 2019]



How do I only get the list of reference index, i.e. [1, 2] One way I guess is to find numbers followed by a dot, but I don't know how.







python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:33







Code Monkey

















asked Nov 23 '18 at 20:23









Code MonkeyCode Monkey

3201211




3201211








  • 2





    Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

    – Wiktor Stribiżew
    Nov 23 '18 at 20:26














  • 2





    Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

    – Wiktor Stribiżew
    Nov 23 '18 at 20:26








2




2





Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

– Wiktor Stribiżew
Nov 23 '18 at 20:26





Try result = list(map(int, re.findall(r"([0-9]+). ", p.text)))

– Wiktor Stribiżew
Nov 23 '18 at 20:26












3 Answers
3






active

oldest

votes


















1














You may use



list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", ref)))


See Python demo:



import re
p_text="""ref = <p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"""
result = list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", p_text)))
print(result) # => [1, 2]


Details





  • (?<![^s>]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a char other than whitespace and >


  • ([0-9]+) - Group 1: one or more digits (it will be the output value from re.findall)


  • . - a . and a space (replace the regular space with s to match any whitespace).


See the regex demo.






share|improve this answer


























  • Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

    – Code Monkey
    Nov 23 '18 at 22:53



















1














You can try this:



import re
o = re.findall(r'[>|s](d{1}).', ref)
print(o)


Will output:



['1', '2']


You might need to define a bit more structure, because just number (digit captured by d) and dot will also capture '8.' at the end of the ISBN number: ISBN:0729537528. Here I used a few characters that (in this example) help distinguishing the two cases. One reference is preceded by a '>' the other one by a space (s).






share|improve this answer































    0














    You have to "escape" the period so something like "[0-9]*." should work. That's off the top of my head so it may be slightly wrong; I'll also leave it up to you to figure out why * is there.



    Be aware that Regex expressions in Python are slightly different from other implementations. For definitive info see:



    See : https://docs.python.org/3/library/re.html



    which suggests that you should start here:



    https://docs.python.org/3/howto/regex.html#regex-howto



    Here's the relevant section of the library page (about 1/ 3 the way down):



    The special sequences consist of '' and a character from the list below. If the ordinary character is not an ASCII digit or an ASCII letter, then the resulting RE will match the second character. For example, $ matches the character '$'.



    For the eqivalent python 2.x page change the version selector found at the top left corner of the page.






    share|improve this answer
























      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
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452564%2fusing-regex-to-find-number-followed-by-dot%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You may use



      list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", ref)))


      See Python demo:



      import re
      p_text="""ref = <p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
      2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"""
      result = list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", p_text)))
      print(result) # => [1, 2]


      Details





      • (?<![^s>]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a char other than whitespace and >


      • ([0-9]+) - Group 1: one or more digits (it will be the output value from re.findall)


      • . - a . and a space (replace the regular space with s to match any whitespace).


      See the regex demo.






      share|improve this answer


























      • Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

        – Code Monkey
        Nov 23 '18 at 22:53
















      1














      You may use



      list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", ref)))


      See Python demo:



      import re
      p_text="""ref = <p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
      2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"""
      result = list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", p_text)))
      print(result) # => [1, 2]


      Details





      • (?<![^s>]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a char other than whitespace and >


      • ([0-9]+) - Group 1: one or more digits (it will be the output value from re.findall)


      • . - a . and a space (replace the regular space with s to match any whitespace).


      See the regex demo.






      share|improve this answer


























      • Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

        – Code Monkey
        Nov 23 '18 at 22:53














      1












      1








      1







      You may use



      list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", ref)))


      See Python demo:



      import re
      p_text="""ref = <p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
      2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"""
      result = list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", p_text)))
      print(result) # => [1, 2]


      Details





      • (?<![^s>]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a char other than whitespace and >


      • ([0-9]+) - Group 1: one or more digits (it will be the output value from re.findall)


      • . - a . and a space (replace the regular space with s to match any whitespace).


      See the regex demo.






      share|improve this answer















      You may use



      list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", ref)))


      See Python demo:



      import re
      p_text="""ref = <p class="references" style="font-size:15px">1. Mcminn. (2003). Last's Anatomy. Elsevier Australia. ISBN:0729537528. <a href="http://books.google.com/books?vid=ISBN0729537528">Read it at Google Books</a> - <a href="http://www.amazon.com/gp/product/0729537528">Find it at Amazon</a><br>
      2. Netter, F. H. (2019). Atlas of human anatomy. Philadelphia, PA: Elsevier.</p>"""
      result = list(map(int, re.findall(r"(?<![^s>])([0-9]+). ", p_text)))
      print(result) # => [1, 2]


      Details





      • (?<![^s>]) - a negative lookbehind that fails the match if, immediately to the left of the current location, there is a char other than whitespace and >


      • ([0-9]+) - Group 1: one or more digits (it will be the output value from re.findall)


      • . - a . and a space (replace the regular space with s to match any whitespace).


      See the regex demo.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 24 '18 at 0:22

























      answered Nov 23 '18 at 20:44









      Wiktor StribiżewWiktor Stribiżew

      330k16149229




      330k16149229













      • Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

        – Code Monkey
        Nov 23 '18 at 22:53



















      • Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

        – Code Monkey
        Nov 23 '18 at 22:53

















      Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

      – Code Monkey
      Nov 23 '18 at 22:53





      Thanks for that! I made a mistake using 'p.text' instead of 'ref' as the variable. I corrected it above.

      – Code Monkey
      Nov 23 '18 at 22:53













      1














      You can try this:



      import re
      o = re.findall(r'[>|s](d{1}).', ref)
      print(o)


      Will output:



      ['1', '2']


      You might need to define a bit more structure, because just number (digit captured by d) and dot will also capture '8.' at the end of the ISBN number: ISBN:0729537528. Here I used a few characters that (in this example) help distinguishing the two cases. One reference is preceded by a '>' the other one by a space (s).






      share|improve this answer




























        1














        You can try this:



        import re
        o = re.findall(r'[>|s](d{1}).', ref)
        print(o)


        Will output:



        ['1', '2']


        You might need to define a bit more structure, because just number (digit captured by d) and dot will also capture '8.' at the end of the ISBN number: ISBN:0729537528. Here I used a few characters that (in this example) help distinguishing the two cases. One reference is preceded by a '>' the other one by a space (s).






        share|improve this answer


























          1












          1








          1







          You can try this:



          import re
          o = re.findall(r'[>|s](d{1}).', ref)
          print(o)


          Will output:



          ['1', '2']


          You might need to define a bit more structure, because just number (digit captured by d) and dot will also capture '8.' at the end of the ISBN number: ISBN:0729537528. Here I used a few characters that (in this example) help distinguishing the two cases. One reference is preceded by a '>' the other one by a space (s).






          share|improve this answer













          You can try this:



          import re
          o = re.findall(r'[>|s](d{1}).', ref)
          print(o)


          Will output:



          ['1', '2']


          You might need to define a bit more structure, because just number (digit captured by d) and dot will also capture '8.' at the end of the ISBN number: ISBN:0729537528. Here I used a few characters that (in this example) help distinguishing the two cases. One reference is preceded by a '>' the other one by a space (s).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 20:49









          deckarddeckard

          31527




          31527























              0














              You have to "escape" the period so something like "[0-9]*." should work. That's off the top of my head so it may be slightly wrong; I'll also leave it up to you to figure out why * is there.



              Be aware that Regex expressions in Python are slightly different from other implementations. For definitive info see:



              See : https://docs.python.org/3/library/re.html



              which suggests that you should start here:



              https://docs.python.org/3/howto/regex.html#regex-howto



              Here's the relevant section of the library page (about 1/ 3 the way down):



              The special sequences consist of '' and a character from the list below. If the ordinary character is not an ASCII digit or an ASCII letter, then the resulting RE will match the second character. For example, $ matches the character '$'.



              For the eqivalent python 2.x page change the version selector found at the top left corner of the page.






              share|improve this answer




























                0














                You have to "escape" the period so something like "[0-9]*." should work. That's off the top of my head so it may be slightly wrong; I'll also leave it up to you to figure out why * is there.



                Be aware that Regex expressions in Python are slightly different from other implementations. For definitive info see:



                See : https://docs.python.org/3/library/re.html



                which suggests that you should start here:



                https://docs.python.org/3/howto/regex.html#regex-howto



                Here's the relevant section of the library page (about 1/ 3 the way down):



                The special sequences consist of '' and a character from the list below. If the ordinary character is not an ASCII digit or an ASCII letter, then the resulting RE will match the second character. For example, $ matches the character '$'.



                For the eqivalent python 2.x page change the version selector found at the top left corner of the page.






                share|improve this answer


























                  0












                  0








                  0







                  You have to "escape" the period so something like "[0-9]*." should work. That's off the top of my head so it may be slightly wrong; I'll also leave it up to you to figure out why * is there.



                  Be aware that Regex expressions in Python are slightly different from other implementations. For definitive info see:



                  See : https://docs.python.org/3/library/re.html



                  which suggests that you should start here:



                  https://docs.python.org/3/howto/regex.html#regex-howto



                  Here's the relevant section of the library page (about 1/ 3 the way down):



                  The special sequences consist of '' and a character from the list below. If the ordinary character is not an ASCII digit or an ASCII letter, then the resulting RE will match the second character. For example, $ matches the character '$'.



                  For the eqivalent python 2.x page change the version selector found at the top left corner of the page.






                  share|improve this answer













                  You have to "escape" the period so something like "[0-9]*." should work. That's off the top of my head so it may be slightly wrong; I'll also leave it up to you to figure out why * is there.



                  Be aware that Regex expressions in Python are slightly different from other implementations. For definitive info see:



                  See : https://docs.python.org/3/library/re.html



                  which suggests that you should start here:



                  https://docs.python.org/3/howto/regex.html#regex-howto



                  Here's the relevant section of the library page (about 1/ 3 the way down):



                  The special sequences consist of '' and a character from the list below. If the ordinary character is not an ASCII digit or an ASCII letter, then the resulting RE will match the second character. For example, $ matches the character '$'.



                  For the eqivalent python 2.x page change the version selector found at the top left corner of the page.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 21:13









                  user1459519user1459519

                  400314




                  400314






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452564%2fusing-regex-to-find-number-followed-by-dot%23new-answer', 'question_page');
                      }
                      );

                      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







                      這個網誌中的熱門文章

                      Hercules Kyvelos

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud