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;
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
migrated from superuser.com Mar 25 at 21:28
This question came from our site for computer enthusiasts and power users.
add a comment |
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
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
add a comment |
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
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
python regex
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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,
add a comment |
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']
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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,
add a comment |
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,
add a comment |
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,
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,
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
add a comment |
add a comment |
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']
add a comment |
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']
add a comment |
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']
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']
answered Mar 25 at 22:43
DSchmidtDSchmidt
5964 silver badges11 bronze badges
5964 silver badges11 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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