Retrieve definition for parenthesized abbreviation, based on letter count

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

How do free-speech protections in the United States apply in public to corporate misrepresentations?

How to trick the reader into thinking they're following a redshirt instead of the protagonist?

Has there been a multiethnic Star Trek character?

A map of non-pathological topology?

Scientist couple raises alien baby

Proving that a Russian cryptographic standard is too structured

A word that means "blending into a community too much"

Why not invest in precious metals?

Should I refuse being named as co-author of a bad quality paper?

Is it expected that a reader will skip parts of what you write?

Is it safe to change the harddrive power feature so that it never turns off?

How can I end combat quickly when the outcome is inevitable?

Longest bridge/tunnel that can be cycled over/through?

Does Assassinate grant two attacks?

Origin of "boor"

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

New bike, tubeless tire will not inflate

Should I put programming books I wrote a few years ago on my resume?

Can a human be transformed into a Mind Flayer?

Excel division by 0 error when trying to average results of formulas

How can I deal with uncomfortable silence from my partner?

Why am I getting a strange double quote (“) in Open Office instead of the ordinary one (")?

How to communicate to my GM that not being allowed to use stealth isn't fun for me?



Retrieve definition for parenthesized abbreviation, based on letter count







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








5















I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:



String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'



Desired output: family health history (FHH), nurse practitioner (NP)



I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.



 import re

a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'

x2 = re.findall('((.*?))', a)

for x in x2:
length = len(x)
print(x, length)









share|improve this question
























  • I think you will need to write some parsing logic here, in addition to maybe using regex.

    – Tim Biegeleisen
    Jun 2 at 2:55











  • I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

    – tenebris silentio
    Jun 2 at 2:59







  • 1





    You should use """ instead of ' for multiline string

    – Keatinge
    Jun 2 at 3:00

















5















I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:



String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'



Desired output: family health history (FHH), nurse practitioner (NP)



I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.



 import re

a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'

x2 = re.findall('((.*?))', a)

for x in x2:
length = len(x)
print(x, length)









share|improve this question
























  • I think you will need to write some parsing logic here, in addition to maybe using regex.

    – Tim Biegeleisen
    Jun 2 at 2:55











  • I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

    – tenebris silentio
    Jun 2 at 2:59







  • 1





    You should use """ instead of ' for multiline string

    – Keatinge
    Jun 2 at 3:00













5












5








5








I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:



String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'



Desired output: family health history (FHH), nurse practitioner (NP)



I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.



 import re

a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'

x2 = re.findall('((.*?))', a)

for x in x2:
length = len(x)
print(x, length)









share|improve this question
















I need to retrieve the definition of an acronym based on the number of letters enclosed in parentheses. For the data I'm dealing with, the number of letters in parentheses corresponds to the number of words to retrieve. I know this isn't a reliable method for getting abbreviations, but in my case it will be. For example:



String = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'



Desired output: family health history (FHH), nurse practitioner (NP)



I know how to extract parentheses from a string, but after that I am stuck. Any help is appreciated.



 import re

a = 'Although family health history (FHH) is commonly accepted as an
important risk factor for common, chronic diseases, it is rarely considered
by a nurse practitioner (NP).'

x2 = re.findall('((.*?))', a)

for x in x2:
length = len(x)
print(x, length)






python regex text text-parsing abbreviation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 2 at 5:54









cdlane

21.4k31245




21.4k31245










asked Jun 2 at 2:45









tenebris silentiotenebris silentio

969




969












  • I think you will need to write some parsing logic here, in addition to maybe using regex.

    – Tim Biegeleisen
    Jun 2 at 2:55











  • I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

    – tenebris silentio
    Jun 2 at 2:59







  • 1





    You should use """ instead of ' for multiline string

    – Keatinge
    Jun 2 at 3:00

















  • I think you will need to write some parsing logic here, in addition to maybe using regex.

    – Tim Biegeleisen
    Jun 2 at 2:55











  • I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

    – tenebris silentio
    Jun 2 at 2:59







  • 1





    You should use """ instead of ' for multiline string

    – Keatinge
    Jun 2 at 3:00
















I think you will need to write some parsing logic here, in addition to maybe using regex.

– Tim Biegeleisen
Jun 2 at 2:55





I think you will need to write some parsing logic here, in addition to maybe using regex.

– Tim Biegeleisen
Jun 2 at 2:55













I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

– tenebris silentio
Jun 2 at 2:59






I know I can run a loop and do a Len(string) to get the number of letters, but I guess it's after that point I'm lost. Like if it's 3 letters, how to capture the previous 3 words.

– tenebris silentio
Jun 2 at 2:59





1




1





You should use """ instead of ' for multiline string

– Keatinge
Jun 2 at 3:00





You should use """ instead of ' for multiline string

– Keatinge
Jun 2 at 3:00












5 Answers
5






active

oldest

votes


















5














Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.



import re
s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


for match in re.finditer(r"((.*?))", s):
start_index = match.start()
abbr = match.group(1)
size = len(abbr)
words = s[:start_index].split()[-size:]
definition = " ".join(words)

print(abbr, definition)


This prints:



FHH family health history
NP nurse practitioner





share|improve this answer

























  • Man, what a life saver. That makes sense. Thanks so much .

    – tenebris silentio
    Jun 2 at 3:09











  • You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

    – MarsNebulaSoup
    Jun 2 at 3:12











  • I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

    – igrinis
    Jun 2 at 13:24


















2














An idea, to use a recursive pattern with PyPI regex module.



b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?


See this pcre demo at regex101




  • b[A-Za-z]+s+ matches a word boundary, one or more alpha, one or more white space


  • (?R)? recursive part: optionally paste the pattern from start


  • (? need to make the parenthesis optional for recursion to fit in )?


  • [A-Z](?=[A-Z]*) match one upper alpha if followed by closing ) with any A-Z in between

  1. Does not check if the first word letter actually match the letter at position in the abbreviation.

  2. Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change [A-Z](?=[A-Z]*)) to (?<=([A-Z]*)[A-Z](?=[A-Z]*)).





share|improve this answer






























    1














    does this solve your problem?



    a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
    splitstr=a.replace('.','').split(' ')
    output=''
    for i,word in enumerate(splitstr):
    if '(' in word:
    w=word.replace('(','').replace(')','').replace('.','')
    for n in range(len(w)+1):
    output=splitstr[i-n]+' '+output

    print(output)


    actually, Keatinge beat me to it






    share|improve this answer






























      0














      Using re with list-comprehension



      x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]

      [re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
      #['family health history (FHH)', 'nurse practitioner (NP)']





      share|improve this answer
































        0














        This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:



        import re

        string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."

        definitions = []

        for acronym in re.findall(r'(([A-Z]+?))', string):
        length = len(acronym)

        match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)

        definitions.append(match.group(0))

        print(", ".join(definitions))


        OUTPUT



        > python3 test.py
        family health history (FHH), nurse practitioner (NP)
        >





        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%2f56411861%2fretrieve-definition-for-parenthesized-abbreviation-based-on-letter-count%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.



          import re
          s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


          for match in re.finditer(r"((.*?))", s):
          start_index = match.start()
          abbr = match.group(1)
          size = len(abbr)
          words = s[:start_index].split()[-size:]
          definition = " ".join(words)

          print(abbr, definition)


          This prints:



          FHH family health history
          NP nurse practitioner





          share|improve this answer

























          • Man, what a life saver. That makes sense. Thanks so much .

            – tenebris silentio
            Jun 2 at 3:09











          • You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

            – MarsNebulaSoup
            Jun 2 at 3:12











          • I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

            – igrinis
            Jun 2 at 13:24















          5














          Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.



          import re
          s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


          for match in re.finditer(r"((.*?))", s):
          start_index = match.start()
          abbr = match.group(1)
          size = len(abbr)
          words = s[:start_index].split()[-size:]
          definition = " ".join(words)

          print(abbr, definition)


          This prints:



          FHH family health history
          NP nurse practitioner





          share|improve this answer

























          • Man, what a life saver. That makes sense. Thanks so much .

            – tenebris silentio
            Jun 2 at 3:09











          • You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

            – MarsNebulaSoup
            Jun 2 at 3:12











          • I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

            – igrinis
            Jun 2 at 13:24













          5












          5








          5







          Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.



          import re
          s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


          for match in re.finditer(r"((.*?))", s):
          start_index = match.start()
          abbr = match.group(1)
          size = len(abbr)
          words = s[:start_index].split()[-size:]
          definition = " ".join(words)

          print(abbr, definition)


          This prints:



          FHH family health history
          NP nurse practitioner





          share|improve this answer















          Use the regex match to find the position of the start of the match. Then use python string indexing to get the substring leading up to the start of the match. Split the substring by words, and get the last n words. Where n is the length of the abbreviation.



          import re
          s = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'


          for match in re.finditer(r"((.*?))", s):
          start_index = match.start()
          abbr = match.group(1)
          size = len(abbr)
          words = s[:start_index].split()[-size:]
          definition = " ".join(words)

          print(abbr, definition)


          This prints:



          FHH family health history
          NP nurse practitioner






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jun 2 at 3:16

























          answered Jun 2 at 3:07









          KeatingeKeatinge

          3,27041532




          3,27041532












          • Man, what a life saver. That makes sense. Thanks so much .

            – tenebris silentio
            Jun 2 at 3:09











          • You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

            – MarsNebulaSoup
            Jun 2 at 3:12











          • I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

            – igrinis
            Jun 2 at 13:24

















          • Man, what a life saver. That makes sense. Thanks so much .

            – tenebris silentio
            Jun 2 at 3:09











          • You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

            – MarsNebulaSoup
            Jun 2 at 3:12











          • I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

            – igrinis
            Jun 2 at 13:24
















          Man, what a life saver. That makes sense. Thanks so much .

          – tenebris silentio
          Jun 2 at 3:09





          Man, what a life saver. That makes sense. Thanks so much .

          – tenebris silentio
          Jun 2 at 3:09













          You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

          – MarsNebulaSoup
          Jun 2 at 3:12





          You can add output = "" to the top of the code, and output += definition + ", (" + abbr + ")" to the end of the loop to get your desired output.

          – MarsNebulaSoup
          Jun 2 at 3:12













          I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

          – igrinis
          Jun 2 at 13:24





          I would suggest to match only capital letters: re.finditer(r"(([A-Z]*?))", s)

          – igrinis
          Jun 2 at 13:24













          2














          An idea, to use a recursive pattern with PyPI regex module.



          b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?


          See this pcre demo at regex101




          • b[A-Za-z]+s+ matches a word boundary, one or more alpha, one or more white space


          • (?R)? recursive part: optionally paste the pattern from start


          • (? need to make the parenthesis optional for recursion to fit in )?


          • [A-Z](?=[A-Z]*) match one upper alpha if followed by closing ) with any A-Z in between

          1. Does not check if the first word letter actually match the letter at position in the abbreviation.

          2. Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change [A-Z](?=[A-Z]*)) to (?<=([A-Z]*)[A-Z](?=[A-Z]*)).





          share|improve this answer



























            2














            An idea, to use a recursive pattern with PyPI regex module.



            b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?


            See this pcre demo at regex101




            • b[A-Za-z]+s+ matches a word boundary, one or more alpha, one or more white space


            • (?R)? recursive part: optionally paste the pattern from start


            • (? need to make the parenthesis optional for recursion to fit in )?


            • [A-Z](?=[A-Z]*) match one upper alpha if followed by closing ) with any A-Z in between

            1. Does not check if the first word letter actually match the letter at position in the abbreviation.

            2. Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change [A-Z](?=[A-Z]*)) to (?<=([A-Z]*)[A-Z](?=[A-Z]*)).





            share|improve this answer

























              2












              2








              2







              An idea, to use a recursive pattern with PyPI regex module.



              b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?


              See this pcre demo at regex101




              • b[A-Za-z]+s+ matches a word boundary, one or more alpha, one or more white space


              • (?R)? recursive part: optionally paste the pattern from start


              • (? need to make the parenthesis optional for recursion to fit in )?


              • [A-Z](?=[A-Z]*) match one upper alpha if followed by closing ) with any A-Z in between

              1. Does not check if the first word letter actually match the letter at position in the abbreviation.

              2. Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change [A-Z](?=[A-Z]*)) to (?<=([A-Z]*)[A-Z](?=[A-Z]*)).





              share|improve this answer













              An idea, to use a recursive pattern with PyPI regex module.



              b[A-Za-z]+s+(?R)?(?[A-Z](?=[A-Z]*)))?


              See this pcre demo at regex101




              • b[A-Za-z]+s+ matches a word boundary, one or more alpha, one or more white space


              • (?R)? recursive part: optionally paste the pattern from start


              • (? need to make the parenthesis optional for recursion to fit in )?


              • [A-Z](?=[A-Z]*) match one upper alpha if followed by closing ) with any A-Z in between

              1. Does not check if the first word letter actually match the letter at position in the abbreviation.

              2. Does not check for an opening parenthesis in front of the abbreviation. To check, add a variable length lookbehind. Change [A-Z](?=[A-Z]*)) to (?<=([A-Z]*)[A-Z](?=[A-Z]*)).






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 2 at 10:42









              bobble bubblebobble bubble

              7,12011529




              7,12011529





















                  1














                  does this solve your problem?



                  a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
                  splitstr=a.replace('.','').split(' ')
                  output=''
                  for i,word in enumerate(splitstr):
                  if '(' in word:
                  w=word.replace('(','').replace(')','').replace('.','')
                  for n in range(len(w)+1):
                  output=splitstr[i-n]+' '+output

                  print(output)


                  actually, Keatinge beat me to it






                  share|improve this answer



























                    1














                    does this solve your problem?



                    a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
                    splitstr=a.replace('.','').split(' ')
                    output=''
                    for i,word in enumerate(splitstr):
                    if '(' in word:
                    w=word.replace('(','').replace(')','').replace('.','')
                    for n in range(len(w)+1):
                    output=splitstr[i-n]+' '+output

                    print(output)


                    actually, Keatinge beat me to it






                    share|improve this answer

























                      1












                      1








                      1







                      does this solve your problem?



                      a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
                      splitstr=a.replace('.','').split(' ')
                      output=''
                      for i,word in enumerate(splitstr):
                      if '(' in word:
                      w=word.replace('(','').replace(')','').replace('.','')
                      for n in range(len(w)+1):
                      output=splitstr[i-n]+' '+output

                      print(output)


                      actually, Keatinge beat me to it






                      share|improve this answer













                      does this solve your problem?



                      a = 'Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP).'
                      splitstr=a.replace('.','').split(' ')
                      output=''
                      for i,word in enumerate(splitstr):
                      if '(' in word:
                      w=word.replace('(','').replace(')','').replace('.','')
                      for n in range(len(w)+1):
                      output=splitstr[i-n]+' '+output

                      print(output)


                      actually, Keatinge beat me to it







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 2 at 3:09









                      3NiGMa3NiGMa

                      559




                      559





















                          0














                          Using re with list-comprehension



                          x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]

                          [re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
                          #['family health history (FHH)', 'nurse practitioner (NP)']





                          share|improve this answer





























                            0














                            Using re with list-comprehension



                            x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]

                            [re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
                            #['family health history (FHH)', 'nurse practitioner (NP)']





                            share|improve this answer



























                              0












                              0








                              0







                              Using re with list-comprehension



                              x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]

                              [re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
                              #['family health history (FHH)', 'nurse practitioner (NP)']





                              share|improve this answer















                              Using re with list-comprehension



                              x_lst = [ str(len(i[1:-1])) for i in re.findall('((.*?))', a) ]

                              [re.search( r'(S+s+)' + i + '(.' + i + ')', a).group(0) for i in x_lst]
                              #['family health history (FHH)', 'nurse practitioner (NP)']






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jun 2 at 3:25

























                              answered Jun 2 at 3:17









                              TranshumanTranshuman

                              2,9761412




                              2,9761412





















                                  0














                                  This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:



                                  import re

                                  string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."

                                  definitions = []

                                  for acronym in re.findall(r'(([A-Z]+?))', string):
                                  length = len(acronym)

                                  match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)

                                  definitions.append(match.group(0))

                                  print(", ".join(definitions))


                                  OUTPUT



                                  > python3 test.py
                                  family health history (FHH), nurse practitioner (NP)
                                  >





                                  share|improve this answer





























                                    0














                                    This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:



                                    import re

                                    string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."

                                    definitions = []

                                    for acronym in re.findall(r'(([A-Z]+?))', string):
                                    length = len(acronym)

                                    match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)

                                    definitions.append(match.group(0))

                                    print(", ".join(definitions))


                                    OUTPUT



                                    > python3 test.py
                                    family health history (FHH), nurse practitioner (NP)
                                    >





                                    share|improve this answer



























                                      0












                                      0








                                      0







                                      This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:



                                      import re

                                      string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."

                                      definitions = []

                                      for acronym in re.findall(r'(([A-Z]+?))', string):
                                      length = len(acronym)

                                      match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)

                                      definitions.append(match.group(0))

                                      print(", ".join(definitions))


                                      OUTPUT



                                      > python3 test.py
                                      family health history (FHH), nurse practitioner (NP)
                                      >





                                      share|improve this answer















                                      This solution isn't particularly clever, it simpy searches for the acronyms and then builds up a pattern to extract the words ahead of each one:



                                      import re

                                      string = "Although family health history (FHH) is commonly accepted as an important risk factor for common, chronic diseases, it is rarely considered by a nurse practitioner (NP)."

                                      definitions = []

                                      for acronym in re.findall(r'(([A-Z]+?))', string):
                                      length = len(acronym)

                                      match = re.search(r'(?:w+W+)' + str(length) + r'(' + acronym + r')', string)

                                      definitions.append(match.group(0))

                                      print(", ".join(definitions))


                                      OUTPUT



                                      > python3 test.py
                                      family health history (FHH), nurse practitioner (NP)
                                      >






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jun 2 at 3:29

























                                      answered Jun 2 at 3:22









                                      cdlanecdlane

                                      21.4k31245




                                      21.4k31245



























                                          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%2f56411861%2fretrieve-definition-for-parenthesized-abbreviation-based-on-letter-count%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

                                          Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

                                          Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

                                          Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림