Match a string which is succeeded by a character but don't match the character itself in regex | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Match a string which is succeeded by a character but don't match the character itself in regex

I have a string "a_c d_" I want to just be able to match the letters using regex that are immediately succeeded by `_`. For example, "a" & "d". I tried using the pattern `\w_`, but it matches the letter as well as the underscore. So the returned values are "a_" & "d_". But I just want to have "a" & "d". How can I do that?

20th Jul 2020, 4:42 PM
XXX
XXX - avatar
2 Answers
+ 5
XXX , lookahead pattern is what you need. Look at the code. Hope it helps you. https://code.sololearn.com/WaeK72RAIoRu/?ref=app
20th Jul 2020, 5:19 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 2
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 quoted from the question `I tried using the pattern '\w_', but it matches the letter as well as the underscore. So the returned values are "a_" & "d_"` I know the guidelines. EDIT: made the question clearer by separating into paragraphs
20th Jul 2020, 4:54 PM
XXX
XXX - avatar