Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi friends,

I need a help in making a regular expression.
I will give a example what i am trying to do.

Suppose consider a sentence,

"This is Simplg an and"

I want a regular expression to replace an with en in word and.
Regex should find an from only and, the complete word an should remain as is.
and i also want to match and replace lg with ly in word simplg

So after replacing my output sentence will be
"This is Simply an end"

Please help me in making regex for these.
Thanks in advance.

What I have tried:

Did not find any such regex on google
Posted
Updated 23-Feb-17 9:21am
v2

\ban(?=d\b)
will match "an" in "and", but not in "an", "hand", etc.
(?<=\bsimp)lg\b
will match "lg" in "simplg", but not in "lg", "bulge", etc.

Regular Expression Language - Quick Reference[^]
Expresso Regular Expression Tool[^]
 
Share this answer
 
Comments
sameer549 23-Feb-17 15:20pm    
hi richard,
thanks for comments, in the second regex u have hardcoded "simp", which will work for simplg but not for other words. Can you make it more helpful, because i am working on find replace kind of thing and user can enter any input.
Richard Deeming 23-Feb-17 15:24pm    
Well, your question did say that you only wanted to match "lg" in "simplg"! :)

If you want to match "lg" at the end of any word, then use:
\Blg\b
sameer549 23-Feb-17 15:31pm    
similarly, can you please make the first regex to work for any word ?
Richard Deeming 23-Feb-17 15:32pm    
\ban\B
sameer549 24-Feb-17 9:09am    
Hey Richard its working fine, Thank you.
Not a solution, but links may be useful.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900