Get the word between Special character

itinfo7
itinfo7 used Ask the Experts™
on
I have string like this for example

string content = "VCA Event is$EventName$$ShelterName$ will be in Texas Hospital";

I want character between "$". and I am not sure how many words I have with "$".

It could be 1: "VCA Event is$EventName$ will be in Texas Hospital";
IT could be 2:"VCA Event is$EventName$$ShelterName$ will be in Texas Hospital";
It could be any numbers and the string between two "$" is also not fixed it could be any character.

Please give me some soultion of this.I am stuck in this one.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
do you want the words between exactly two $ signs, or do you want all the words in the string except dollar sign....

please clarify...
Top Expert 2011
Commented:
Please try:
var rx = new Regex(@"(?<=\$)[^\$]+?(?=\$)");
string content = "VCA Event is$EventName$$ShelterName$ will be in Texas Hospital";

string[] keywords = rx.Matches(content).Cast<Match>().Select( _ => _.Value).ToArray();

/* Output

EventName
ShelterName

*/

Open in new window

Author

Commented:
your solution is perfectly worked.but for one scenario
 if string content = "VCA Event is$EventName$Name$ShelterName$ will be in Texas Hospital";

In this can I don't want Name but Name is in between $$ it's giving me. three keywords
EventName,Name,helterName
Top Expert 2011

Commented:
OK.  Please update the code as follows:
var rx = new Regex(@"\$([^\$]+?)\$");
string content = "VCA Event is$EventName$Name$ShelterName$ will be in Texas Hospital";

string[] keywords = rx.Matches(content).Cast<Match>().Select( _ => _.Groups[1].Value).ToArray();

/* Output

EventName
ShelterName

*/

Open in new window

Author

Commented:
yeah it worked...Thanks for helping me out..........Reallly appriciate your help..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial