python regrex not able to fetch data












0















Problem : My objective is to fetch all the phone numbers from file.
I am able to fetch all the phones except the one for user named as "suneja, amit" present in the second to last line in the data file.
I was able to fetch it till step3 of code where I have used 3 groups. But it is not coming up when I tried to use 4th group.



Here is Data File:



Love, Kenneth   kenneth@teamtreehouse.com   +1 (555) 555-5555   Teacher, Treehouse  @kennethlove
McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse
Arthur, King king_arthur@camelot.co.uk King, Camelot
Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik
, Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave
Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson
Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey
Exampleson, Example me@example.com +1-555-555-5552 Example, Example Co. @example
Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44
Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers
Vader, Darth darth-vader@empire.gov (555).555.4444 Sith Lord, Galactic Empire @darthvader
suneja, amit amit.suneja007@gmail.com 444-444444 B102, City Center @programmer
Fernndez de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.


Here is my code:



import re
data_file = 'names.txt'

with open(data_file, 'r', encoding="utf-8") as myfile:
data_dump = myfile.read()

print("___________________________________")
print(re.findall(r"(+d[-s])", data_dump))
print("___________________________________")
print(re.findall(r"(+d[s-])?((?d{3})?)", data_dump))
print("___________________________________")
print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})", data_dump))
print("___________________________________")
print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump))
print(len(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump)))


Here is my Output of Code:



___________________________________
['+1 ', '+1-']
___________________________________
[('+1 ', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '554'), ('+1-', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '444'), ('', '007'), ('', '444'), ('', '444'), ('', '444'), ('', '102')]
___________________________________
[('+1 ', '(555)', ' 555'), ('', '(555)', ' 555'), ('', '(555)', ' 555'), ('+1-', '555', '-555'), ('', '555', ' 555'), ('', '(555)', ' 555'), ('', '(555)', '.555'), ('', '444', '-444')]
___________________________________
[('+1 ', '(555)', ' 555', '-5555'), ('', '(555)', ' 555', '-5554'), ('', '(555)', ' 555', '-5543'), ('+1-', '555', '-555', '-5552'), ('', '555', ' 555', '-5551'), ('', '(555)', ' 555', '-5553'), ('', '(555)', '.555', '.4444')]
7









share|improve this question





























    0















    Problem : My objective is to fetch all the phone numbers from file.
    I am able to fetch all the phones except the one for user named as "suneja, amit" present in the second to last line in the data file.
    I was able to fetch it till step3 of code where I have used 3 groups. But it is not coming up when I tried to use 4th group.



    Here is Data File:



    Love, Kenneth   kenneth@teamtreehouse.com   +1 (555) 555-5555   Teacher, Treehouse  @kennethlove
    McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse
    Arthur, King king_arthur@camelot.co.uk King, Camelot
    Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik
    , Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave
    Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson
    Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey
    Exampleson, Example me@example.com +1-555-555-5552 Example, Example Co. @example
    Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44
    Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers
    Vader, Darth darth-vader@empire.gov (555).555.4444 Sith Lord, Galactic Empire @darthvader
    suneja, amit amit.suneja007@gmail.com 444-444444 B102, City Center @programmer
    Fernndez de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.


    Here is my code:



    import re
    data_file = 'names.txt'

    with open(data_file, 'r', encoding="utf-8") as myfile:
    data_dump = myfile.read()

    print("___________________________________")
    print(re.findall(r"(+d[-s])", data_dump))
    print("___________________________________")
    print(re.findall(r"(+d[s-])?((?d{3})?)", data_dump))
    print("___________________________________")
    print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})", data_dump))
    print("___________________________________")
    print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump))
    print(len(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump)))


    Here is my Output of Code:



    ___________________________________
    ['+1 ', '+1-']
    ___________________________________
    [('+1 ', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '554'), ('+1-', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '444'), ('', '007'), ('', '444'), ('', '444'), ('', '444'), ('', '102')]
    ___________________________________
    [('+1 ', '(555)', ' 555'), ('', '(555)', ' 555'), ('', '(555)', ' 555'), ('+1-', '555', '-555'), ('', '555', ' 555'), ('', '(555)', ' 555'), ('', '(555)', '.555'), ('', '444', '-444')]
    ___________________________________
    [('+1 ', '(555)', ' 555', '-5555'), ('', '(555)', ' 555', '-5554'), ('', '(555)', ' 555', '-5543'), ('+1-', '555', '-555', '-5552'), ('', '555', ' 555', '-5551'), ('', '(555)', ' 555', '-5553'), ('', '(555)', '.555', '.4444')]
    7









    share|improve this question



























      0












      0








      0








      Problem : My objective is to fetch all the phone numbers from file.
      I am able to fetch all the phones except the one for user named as "suneja, amit" present in the second to last line in the data file.
      I was able to fetch it till step3 of code where I have used 3 groups. But it is not coming up when I tried to use 4th group.



      Here is Data File:



      Love, Kenneth   kenneth@teamtreehouse.com   +1 (555) 555-5555   Teacher, Treehouse  @kennethlove
      McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse
      Arthur, King king_arthur@camelot.co.uk King, Camelot
      Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik
      , Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave
      Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson
      Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey
      Exampleson, Example me@example.com +1-555-555-5552 Example, Example Co. @example
      Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44
      Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers
      Vader, Darth darth-vader@empire.gov (555).555.4444 Sith Lord, Galactic Empire @darthvader
      suneja, amit amit.suneja007@gmail.com 444-444444 B102, City Center @programmer
      Fernndez de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.


      Here is my code:



      import re
      data_file = 'names.txt'

      with open(data_file, 'r', encoding="utf-8") as myfile:
      data_dump = myfile.read()

      print("___________________________________")
      print(re.findall(r"(+d[-s])", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump))
      print(len(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump)))


      Here is my Output of Code:



      ___________________________________
      ['+1 ', '+1-']
      ___________________________________
      [('+1 ', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '554'), ('+1-', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '444'), ('', '007'), ('', '444'), ('', '444'), ('', '444'), ('', '102')]
      ___________________________________
      [('+1 ', '(555)', ' 555'), ('', '(555)', ' 555'), ('', '(555)', ' 555'), ('+1-', '555', '-555'), ('', '555', ' 555'), ('', '(555)', ' 555'), ('', '(555)', '.555'), ('', '444', '-444')]
      ___________________________________
      [('+1 ', '(555)', ' 555', '-5555'), ('', '(555)', ' 555', '-5554'), ('', '(555)', ' 555', '-5543'), ('+1-', '555', '-555', '-5552'), ('', '555', ' 555', '-5551'), ('', '(555)', ' 555', '-5553'), ('', '(555)', '.555', '.4444')]
      7









      share|improve this question
















      Problem : My objective is to fetch all the phone numbers from file.
      I am able to fetch all the phones except the one for user named as "suneja, amit" present in the second to last line in the data file.
      I was able to fetch it till step3 of code where I have used 3 groups. But it is not coming up when I tried to use 4th group.



      Here is Data File:



      Love, Kenneth   kenneth@teamtreehouse.com   +1 (555) 555-5555   Teacher, Treehouse  @kennethlove
      McFarland, Dave dave@teamtreehouse.com (555) 555-5554 Teacher, Treehouse
      Arthur, King king_arthur@camelot.co.uk King, Camelot
      Österberg, Sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik
      , Tim tim@killerrabbit.com Enchanter, Killer Rabbit Cave
      Carson, Ryan ryan@teamtreehouse.com (555) 555-5543 CEO, Treehouse @ryancarson
      Doctor, The doctor+companion@tardis.co.uk Time Lord, Gallifrey
      Exampleson, Example me@example.com +1-555-555-5552 Example, Example Co. @example
      Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44
      Chalkley, Andrew andrew@teamtreehouse.com (555) 555-5553 Teacher, Treehouse @chalkers
      Vader, Darth darth-vader@empire.gov (555).555.4444 Sith Lord, Galactic Empire @darthvader
      suneja, amit amit.suneja007@gmail.com 444-444444 B102, City Center @programmer
      Fernndez de la Vega Sanz, María Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.


      Here is my code:



      import re
      data_file = 'names.txt'

      with open(data_file, 'r', encoding="utf-8") as myfile:
      data_dump = myfile.read()

      print("___________________________________")
      print(re.findall(r"(+d[-s])", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})", data_dump))
      print("___________________________________")
      print(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump))
      print(len(re.findall(r"(+d[s-])?((?d{3})?)([s-.]d{3})([s.-]d{4,6})", data_dump)))


      Here is my Output of Code:



      ___________________________________
      ['+1 ', '+1-']
      ___________________________________
      [('+1 ', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '554'), ('+1-', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '555'), ('', '(555)'), ('', '555'), ('', '444'), ('', '007'), ('', '444'), ('', '444'), ('', '444'), ('', '102')]
      ___________________________________
      [('+1 ', '(555)', ' 555'), ('', '(555)', ' 555'), ('', '(555)', ' 555'), ('+1-', '555', '-555'), ('', '555', ' 555'), ('', '(555)', ' 555'), ('', '(555)', '.555'), ('', '444', '-444')]
      ___________________________________
      [('+1 ', '(555)', ' 555', '-5555'), ('', '(555)', ' 555', '-5554'), ('', '(555)', ' 555', '-5543'), ('+1-', '555', '-555', '-5552'), ('', '555', ' 555', '-5551'), ('', '(555)', ' 555', '-5553'), ('', '(555)', '.555', '.4444')]
      7






      python regex-group






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 2:25









      Pedro Torres

      703413




      703413










      asked Nov 26 '18 at 2:05









      user8588795user8588795

      967




      967
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You only need a little change on your last regex to make it work:



          (+d[s-])?((?d{3})?)([s-.]d{3})([s.-]?d{3,6})


          The change was only in the last capture group putting the question mark in: ([s.-]?d{3,6})



          The question mark in that group makes the [s.-] optional. Since your last phone number doesn't have any of those characters, they have to be optional






          share|improve this answer


























          • Thanks it worked for me . Can you explain the the change you made in my code ?

            – user8588795
            Nov 26 '18 at 8:05











          • Updated the answer with the explanation

            – Pedro Torres
            Nov 26 '18 at 12:12











          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%2f53473909%2fpython-regrex-not-able-to-fetch-data%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You only need a little change on your last regex to make it work:



          (+d[s-])?((?d{3})?)([s-.]d{3})([s.-]?d{3,6})


          The change was only in the last capture group putting the question mark in: ([s.-]?d{3,6})



          The question mark in that group makes the [s.-] optional. Since your last phone number doesn't have any of those characters, they have to be optional






          share|improve this answer


























          • Thanks it worked for me . Can you explain the the change you made in my code ?

            – user8588795
            Nov 26 '18 at 8:05











          • Updated the answer with the explanation

            – Pedro Torres
            Nov 26 '18 at 12:12
















          0














          You only need a little change on your last regex to make it work:



          (+d[s-])?((?d{3})?)([s-.]d{3})([s.-]?d{3,6})


          The change was only in the last capture group putting the question mark in: ([s.-]?d{3,6})



          The question mark in that group makes the [s.-] optional. Since your last phone number doesn't have any of those characters, they have to be optional






          share|improve this answer


























          • Thanks it worked for me . Can you explain the the change you made in my code ?

            – user8588795
            Nov 26 '18 at 8:05











          • Updated the answer with the explanation

            – Pedro Torres
            Nov 26 '18 at 12:12














          0












          0








          0







          You only need a little change on your last regex to make it work:



          (+d[s-])?((?d{3})?)([s-.]d{3})([s.-]?d{3,6})


          The change was only in the last capture group putting the question mark in: ([s.-]?d{3,6})



          The question mark in that group makes the [s.-] optional. Since your last phone number doesn't have any of those characters, they have to be optional






          share|improve this answer















          You only need a little change on your last regex to make it work:



          (+d[s-])?((?d{3})?)([s-.]d{3})([s.-]?d{3,6})


          The change was only in the last capture group putting the question mark in: ([s.-]?d{3,6})



          The question mark in that group makes the [s.-] optional. Since your last phone number doesn't have any of those characters, they have to be optional







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 26 '18 at 12:12

























          answered Nov 26 '18 at 2:25









          Pedro TorresPedro Torres

          703413




          703413













          • Thanks it worked for me . Can you explain the the change you made in my code ?

            – user8588795
            Nov 26 '18 at 8:05











          • Updated the answer with the explanation

            – Pedro Torres
            Nov 26 '18 at 12:12



















          • Thanks it worked for me . Can you explain the the change you made in my code ?

            – user8588795
            Nov 26 '18 at 8:05











          • Updated the answer with the explanation

            – Pedro Torres
            Nov 26 '18 at 12:12

















          Thanks it worked for me . Can you explain the the change you made in my code ?

          – user8588795
          Nov 26 '18 at 8:05





          Thanks it worked for me . Can you explain the the change you made in my code ?

          – user8588795
          Nov 26 '18 at 8:05













          Updated the answer with the explanation

          – Pedro Torres
          Nov 26 '18 at 12:12





          Updated the answer with the explanation

          – Pedro Torres
          Nov 26 '18 at 12:12




















          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%2f53473909%2fpython-regrex-not-able-to-fetch-data%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







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin