Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Can anybody help me to get the solution using regular expression in c#
basically i have to add some text before *p0x0Y pattern . In this pattern p,x,y will be fix value and 0 can be replace by any number .

i have to edit a
C#
string str= "l8c1E*p0x0Y"

In this string *p0x0Y can be *p1x1Y or *p2x2Y .

and after editing output should be according *p0x0Y format

C#
string output = "18c1EXXX*p0x0Y"


Thanks in advance .
Posted

1 solution

Try:
C#
string str = "l8c1E*p0x0Y";
string output = Regex.Replace(str, @"(\*p\dx\dY)", @"XXX$1");



"Hi OriginalGriff , Your solution worked for me , now i want to ask one more thing ... i have to replace left side string into right side string in below text .. In this text "left arrow sign" is one symbol which i could not copy paste so i wrote it in text ,its just one symbol. So please give me regular expression code to replace it . "left arrow sign"*p0x0Y --> "left arrow sign"xxx "left arrow sign"*p0x0Y Am i clear about my requirement , Original ?"


Without knowing the exact character, I can't be sure, but...the dot character matches anything, so it might be that what you want is:
C#
string output = Regex.Replace(str, @"(.)(\*p\dx\dY)", @"$1XXX$1$2");

Give it a try. In the replacement code "$n" indicates a numbered match group from the match string, so "$1XXX$1" should give you "your-character XXX your-character" in the output.

If you are going to use regexes much, I'd recommend you get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
v2
Comments
lalit.mca2006 9-Oct-13 6:08am    
Thank you so much OriginalGriff..
OriginalGriff 9-Oct-13 6:17am    
You're welcome!
lalit.mca2006 9-Oct-13 7:09am    
Hi OriginalGriff ,
Your solution worked for me , now i want to ask one more thing ...
i have to replace left side string into right side string in below text ..
In this text "left arrow sign" is one symbol which i could not copy paste so i wrote it in text ,its just one symbol. So please give me regular expression code to replace it .

"left arrow sign"*p0x0Y --> "left arrow sign"xxx"left arrow sign"*p0x0Y

Am i clear about my requirement , Original ?




OriginalGriff 9-Oct-13 7:25am    
Answer updated
lalit.mca2006 9-Oct-13 7:36am    
Thanks a lot for quick and accurate reply which i needed .
by the way this "left arrow sign" is one specific character , which looks like a "left arrow" in notepad and looks like "ESC" in notepad++.
when i tried to copy-paste , it was not showing at the time of paste .
but your solution (code) is much better because it takes care all things .

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