Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, i wanna ask about some Regular Expression, though i'm not quite understand at regular Expression.

i wanna to know about the pattern for this string,

here is the String :

1. $BUMI
2. $BUMI,
3. $BUMI-W


and for those string, i want to change it become like this in c#

1. $<a href="BUMI">BUMI</a>
2. $<a href="BUMI">BUMI.</a>
3. $<a href="BUMI-W">BUMI-W</a>


thanks for the answer.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 25-Mar-15 0:14am
v2
Comments
Matt T Heffron 25-Mar-15 20:02pm    
Do the input strings ALWAYS start with the $ character?
Or is it number, dot, space, $
Or ???
You need to tell us EXACTLY what the input patterns can be.

Obviously, not EVERY POSSIBLE input... but describe the patterns.
What is fixed?
What is constrained? (e.g., number only, or at least n-characters long, or ...)
What is variable?
Andrew Budiman 25-Mar-15 23:48pm    
hi Matt T Heffron,

the input string is always start with the $ character
as of you said, after the $ character it can be 4 alphabet, and then, it can follow by - or . after - it can be followed by 1 alphabet.

so let me give an example..

$BUMI this is cool, <- $BUMI this is cool
$BUMI. this is cool <- $BUMI. this is cool
$BUMI-W this is cool <- $BUMI-W this is cool

the alphabet after BUMI, can be anything (4 alphabet or with -(1 alphabet(toUpper)) )

1 solution

Just use
(.*?)(BUMI.*)$
And then use Regex.Replace:
C#
string outp = Regex.Replace(inp, @"(.*?)(BUMI.*)$", @"${1}<a href="${2}">${2}</a>");


"thanks for you answer, but the BUMI can be other word, maybe some kind of string, like BRMS, or BMRI."

So try this:
C#
string outp = Regex.Replace(inp, @"\$(BUMI.*)$", @"\$<a href="${1}">${1}</a>");
 
Share this answer
 
v2
Comments
Andrew Budiman 25-Mar-15 6:23am    
thanks for you answer, but the BUMI can be other word, maybe some kind of string, like BRMS, or BMRI.
OriginalGriff 25-Mar-15 6:30am    
So replace it...
OriginalGriff 25-Mar-15 6:33am    
Answer updated
But if you need something specific, ask for something specific: remember that we can't see your screen, access your HDD, or read your mind! :laugh:
Andrew Budiman 25-Mar-15 6:32am    
how do i replace it ?
OriginalGriff 25-Mar-15 6:36am    
I'd suggest that you need to learn about Regexes: Get a copy of Expresso

http://www.ultrapico.com/Expresso.htm

it's free, and it examines and generates Regular expressions.

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