Regex expression to find keywords in a string along with following charactersWhat is the best regular expression to check if a string is a valid URL?What special characters must be escaped in regular expressions?Count the number occurrences of a character in a stringFind and kill a process in one line using bash and regexHow can I print literal curly-brace characters in python string and also use .format on it?Regex Match all characters between two stringsCheck whether a string matches a regex in JSHow to split a string or file that may be delimited by a combination of comments and spaces, tabs, newlines, commas, or other charactersHow to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopspython regex get value after string

A horrible Stockfish chess engine evaluation

What is a "Lear Processor" and how did it work?

Why do we need common sense in AI?

Why is the ladder of the LM always in the dark side of the LM?

Why does wrapping Aluminium foil around my food help it keep warm, aluminium be good conductor should have no effect?

Having decision making power over someone's assets

Why do you use the "park" gear to park a car and not only the handbrake?

Addressing unnecessary daily meetings with manager?

What are some further readings in Econometrics you recommend?

How are mathematicians paid to do research?

Yet another hash table in C

What the real concept of Static keyword in perspective of Embedded C. See below code

Through: how to use it with subtraction of functions?

Why does the Antonov AN-225 not have any winglets?

What happens to unproductive professors?

Given a 32 bit number, what is an efficient way to scale each byte by a certain factor?

Is there any reason why MCU changed the Snap to Blip

Would a carnivorous diet be able to support a giant worm?

LMOP: what beasts live in Neverwinter Wood?

Intelligent Ants in the Amazon

How often does the spell Sleet Storm require concentration checks?

Aura vs enchantment

Is this a reference to the film Alien in the novel 2010 Odyssey Two?

What is this little owl-like bird?



Regex expression to find keywords in a string along with following characters


What is the best regular expression to check if a string is a valid URL?What special characters must be escaped in regular expressions?Count the number occurrences of a character in a stringFind and kill a process in one line using bash and regexHow can I print literal curly-brace characters in python string and also use .format on it?Regex Match all characters between two stringsCheck whether a string matches a regex in JSHow to split a string or file that may be delimited by a combination of comments and spaces, tabs, newlines, commas, or other charactersHow to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loopspython regex get value after string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I have a bunch of known keywords, like 'cat', 'dog', 'rat'.



I have a string that can be something like this:



'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey'


I need to find all the keywords plus any characters that are immediately following them (not including delimiters like semicolon and comma as well as the next keyword).



So the result from the string above should be this list:



'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey'


I've tried a few ways but end up getting items that contain multiple keywords, or missing some keywords.










share|improve this question















migrated from superuser.com Mar 25 at 21:28


This question came from our site for computer enthusiasts and power users.













  • 2





    It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

    – cybernetic.nomad
    Mar 25 at 16:50











  • is it always and only rat, cat and dog for the 'main' keywords?

    – Tacratis
    Mar 25 at 21:31

















0















I have a bunch of known keywords, like 'cat', 'dog', 'rat'.



I have a string that can be something like this:



'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey'


I need to find all the keywords plus any characters that are immediately following them (not including delimiters like semicolon and comma as well as the next keyword).



So the result from the string above should be this list:



'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey'


I've tried a few ways but end up getting items that contain multiple keywords, or missing some keywords.










share|improve this question















migrated from superuser.com Mar 25 at 21:28


This question came from our site for computer enthusiasts and power users.













  • 2





    It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

    – cybernetic.nomad
    Mar 25 at 16:50











  • is it always and only rat, cat and dog for the 'main' keywords?

    – Tacratis
    Mar 25 at 21:31













0












0








0








I have a bunch of known keywords, like 'cat', 'dog', 'rat'.



I have a string that can be something like this:



'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey'


I need to find all the keywords plus any characters that are immediately following them (not including delimiters like semicolon and comma as well as the next keyword).



So the result from the string above should be this list:



'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey'


I've tried a few ways but end up getting items that contain multiple keywords, or missing some keywords.










share|improve this question
















I have a bunch of known keywords, like 'cat', 'dog', 'rat'.



I have a string that can be something like this:



'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey'


I need to find all the keywords plus any characters that are immediately following them (not including delimiters like semicolon and comma as well as the next keyword).



So the result from the string above should be this list:



'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey'


I've tried a few ways but end up getting items that contain multiple keywords, or missing some keywords.







python regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 26 at 13:38









ZygD

4,46911 gold badges30 silver badges54 bronze badges




4,46911 gold badges30 silver badges54 bronze badges










asked Mar 25 at 15:24









catdogratcatdograt

1




1




migrated from superuser.com Mar 25 at 21:28


This question came from our site for computer enthusiasts and power users.









migrated from superuser.com Mar 25 at 21:28


This question came from our site for computer enthusiasts and power users.









  • 2





    It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

    – cybernetic.nomad
    Mar 25 at 16:50











  • is it always and only rat, cat and dog for the 'main' keywords?

    – Tacratis
    Mar 25 at 21:31












  • 2





    It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

    – cybernetic.nomad
    Mar 25 at 16:50











  • is it always and only rat, cat and dog for the 'main' keywords?

    – Tacratis
    Mar 25 at 21:31







2




2





It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

– cybernetic.nomad
Mar 25 at 16:50





It would be helpful if you could edit your question to include the expected results, what you have tried so far (something more specific than "a few ways") and how it fails to meet expectations

– cybernetic.nomad
Mar 25 at 16:50













is it always and only rat, cat and dog for the 'main' keywords?

– Tacratis
Mar 25 at 21:31





is it always and only rat, cat and dog for the 'main' keywords?

– Tacratis
Mar 25 at 21:31












2 Answers
2






active

oldest

votes


















0














Try this



>>> [(f[0], f[1][0]) for f in re.findall(r'(rat|dog|cat)(.w+)', 'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey') if re.match(r'(cat|dog|rat|[,;])', f[1]) is None]
[('cat', ':')]


Regards,






share|improve this answer
































    0














    I think it is not possible using pure regex.
    A trick would be to add a deliminator before all keywords and to split afterwards.



    >>> s = "rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey"
    >>> re.split(r"[;,]+", re.sub(r"(cat|dog|rat)",r",1",s))
    ['', 'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey']





    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%2f55346674%2fregex-expression-to-find-keywords-in-a-string-along-with-following-characters%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try this



      >>> [(f[0], f[1][0]) for f in re.findall(r'(rat|dog|cat)(.w+)', 'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey') if re.match(r'(cat|dog|rat|[,;])', f[1]) is None]
      [('cat', ':')]


      Regards,






      share|improve this answer





























        0














        Try this



        >>> [(f[0], f[1][0]) for f in re.findall(r'(rat|dog|cat)(.w+)', 'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey') if re.match(r'(cat|dog|rat|[,;])', f[1]) is None]
        [('cat', ':')]


        Regards,






        share|improve this answer



























          0












          0








          0







          Try this



          >>> [(f[0], f[1][0]) for f in re.findall(r'(rat|dog|cat)(.w+)', 'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey') if re.match(r'(cat|dog|rat|[,;])', f[1]) is None]
          [('cat', ':')]


          Regards,






          share|improve this answer















          Try this



          >>> [(f[0], f[1][0]) for f in re.findall(r'(rat|dog|cat)(.w+)', 'rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey') if re.match(r'(cat|dog|rat|[,;])', f[1]) is None]
          [('cat', ':')]


          Regards,







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 22:22

























          answered Mar 25 at 22:03









          geckosgeckos

          1,5981 gold badge13 silver badges26 bronze badges




          1,5981 gold badge13 silver badges26 bronze badges























              0














              I think it is not possible using pure regex.
              A trick would be to add a deliminator before all keywords and to split afterwards.



              >>> s = "rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey"
              >>> re.split(r"[;,]+", re.sub(r"(cat|dog|rat)",r",1",s))
              ['', 'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey']





              share|improve this answer



























                0














                I think it is not possible using pure regex.
                A trick would be to add a deliminator before all keywords and to split afterwards.



                >>> s = "rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey"
                >>> re.split(r"[;,]+", re.sub(r"(cat|dog|rat)",r",1",s))
                ['', 'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey']





                share|improve this answer

























                  0












                  0








                  0







                  I think it is not possible using pure regex.
                  A trick would be to add a deliminator before all keywords and to split afterwards.



                  >>> s = "rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey"
                  >>> re.split(r"[;,]+", re.sub(r"(cat|dog|rat)",r",1",s))
                  ['', 'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey']





                  share|improve this answer













                  I think it is not possible using pure regex.
                  A trick would be to add a deliminator before all keywords and to split afterwards.



                  >>> s = "rat;dog,cat - browncatdog(black)ratratcatdog-whitecat:grey"
                  >>> re.split(r"[;,]+", re.sub(r"(cat|dog|rat)",r",1",s))
                  ['', 'rat', 'dog', 'cat - brown', 'cat', 'dog(black)', 'rat', 'rat', 'cat', 'dog-white', 'cat:grey']






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 22:43









                  DSchmidtDSchmidt

                  5964 silver badges11 bronze badges




                  5964 silver badges11 bronze badges



























                      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%2f55346674%2fregex-expression-to-find-keywords-in-a-string-along-with-following-characters%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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript