Click here to Skip to main content
15,881,872 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone,

i want to ask about a regex pattern,
the example of the string is : "This is So Good $AAAA" or it can be "This is so Good $AAAA & $BBBB"

in that example, i want to retrieve and change the $AAAA and $BBBB to an link,
and the link format is http://webname/name.aspx?name=AAAA or name=BBBB

thanks.
Posted

So do it:
\$(?<postdollar>[\w]+)(?<term>\s|$)</term></postdollar>
Will extract the AAAA or BBBB into separate Matches: you can then use replace to change it:
C#
public static Regex regex = new Regex(
      "\\$(?<PostDollar>[\\w]+)(?<Term>\\s|$)",
    RegexOptions.IgnoreCase
    | RegexOptions.Multiline
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
public static string regexReplace = "http://webname/name.aspx?name=${PostDollar}";

string result = regex.Replace(inputText,regexReplace);
 
Share this answer
 
Hi Anrew, modified as per your clarification:
Input: "This is so Good $AAAA & $BBBB"
Output: This is So Good AAAA and AABB
C#
Regex reg = new Regex(@"\$[A-Z]*");
string input = "This is So Good $AAAA and $AABB";
string hyperlink;
string value;
string output = input;
foreach (var item in reg.Matches(input))
{
    value = item.ToString().Remove(0,1); // to remove the "$" character
    hyperlink = string.Format("<a href='http://webname/name.aspx?name={0}'>{0}</a>", value);
    output = output.Replace(item.ToString(), hyperlink);
}
Console.WriteLine(output);
//This is So Good <a href='http://webname/name.aspx?name=AAAA'>AAAA</a> and <a href='http://webname/name.aspx?name=AABB'>AABB</a>
 
Share this answer
 
v3
Comments
Andrew Budiman 17-Jun-14 5:51am    
that's not what i mean, what i mean is like this.
ex :
string s1 = "this is so Good $AABB and $BBCC".

it become "this is so Good $AABB and $BBCC"
Debabrata_Das 17-Jun-14 8:22am    
Hi, I've modified my solution. Please have a look and let me know if it works for you.
- DD
Andrew Budiman 17-Jun-14 9:41am    
thx, it worked sir. :D
hehehe...
Debabrata_Das 20-Jun-14 0:15am    
Hey Andrew, It's good to hear that! I would request you to accept this solution as answer. You can rate this too ;)
Andrew Budiman 9-Apr-15 4:56am    
hi sir,
can i contact you more ? it's been 1 year since i asked this question. :D

here i got some problem, like this.
the string is now come out like this :

* oke, here $BUMI. <- oke here $BUMI.
* oke, here $BUMI-W <- oke here $BUMI
* oke, here $BUMI <- oke here $BUMI

the word BUMI can be anything, it can be BRMS, GOOG, CEBU, and more.
please help me sir..
thanks for your attention

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