-2

I have a string like this

ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH

How do I retrieve the string between $ and /$?

Expected string should be

  1. ToBeFetched1

  2. ToBeFetched2

Max
  • 12,622
  • 16
  • 73
  • 101
Venkatesh
  • 19
  • 1
  • 4
  • 2
    Welcome to SO. http://whathaveyoutried.com? Please show us what you have so far. SO is not a code writing service, and you will get a better response if you provide evidence of your own work. Please see [the Help pages](http://stackoverflow.com/help). – freefaller Sep 19 '13 at 08:56
  • Tried with this \$([^$]*)\ /$ – Venkatesh Sep 19 '13 at 08:56
  • 2
    update your question with what you have tried. You are almost there. Maybe you can try : \$([^$]*)\/\$ – jbl Sep 19 '13 at 09:06
  • Please clarify: do you want specifically a Regex-based solution? Or are you interested in some specific programming language or processing tool? – quetzalcoatl Sep 19 '13 at 09:17
  • @LuisFilipe: I've just rolled it back. The .Net context is relevant as Regex engines/dialects are different between platforms. – quetzalcoatl Sep 19 '13 at 09:21
  • possible duplicate of [Regex Match all characters between two strings](http://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings) – rebeliagamer Sep 19 '13 at 09:32

4 Answers4

2
Regex r = new Regex(Regex.Escape("-$") + "(.*?)"  + Regex.Escape(@"/$"));
                MatchCollection matches = r.Matches("ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH");
            foreach (Match match in matches)
            {
                Console.WriteLine(match.Groups[1].Value);
            }

Here this will work for sure.

Anirudh Agarwal
  • 655
  • 2
  • 7
  • 29
2

Since you have "open" and "close" markers, the regex expression would obviously be built around this form:

[head-marker: $] [the content you are interested in: anything] [tail-marker: /$]

so, with addition of a parentheses to form a capturing group:

$(.*)$

However, two problems here: * expressions are greedy (and you dont want it to be, as you want all smallest matches possible) - so it has to be weakened, and also the $ is a special character in regex, so it has to be escaped:

\$(.*?)/\$

this forms almost-good expression. It will however falsely match agains such input:

aaaaa/$bbbbb/$ccccc    ->    bbbbb

because the "head-marker" can skip the slash and hit the first dollar sign what most probably you wouldn't want. Hence, some lookbehind would be useful here too:

(?!</)\$(.*?)/\$

The ?!<XXXX instructs to match only if XXXX does not precede the potential match.

See also MSDN: Regex syntax and operators

edit: actually Arie's suggestion is much simplier as it doesn't use capturing group. Note the small difference though: Arie's example explicitely fobids the data to contain a dollar sign, so ABCD$-$ToBeFe$tched1/$- will result in tched1 not ToBeFe$tched1. If you need the latter, just change the inner [^$] part. Think and pick what you actually need!

quetzalcoatl
  • 32,194
  • 8
  • 68
  • 107
1

Using String Methods:

string s ="ABCD$-$ToBeFetched1/$-$ToBeFetched2/$-EF$GH";

var results = s.Split('-')
               .Where(x=> x.StartsWith("$") && x.EndsWith("/$"))
               .Select(y=> y.Substring(1,y.Length - 3));

//Console.WriteLine("string1: {0}, string2:{1}",results.ToArray());
Kaf
  • 33,101
  • 7
  • 58
  • 78
  • Indeed! I've just rolled it back. The .Net context is relevant as Regex engines/dialects are different between platforms. – quetzalcoatl Sep 19 '13 at 09:20
1

(?<=\$)[^$]{1,}(?=/\$)

(?<=\$) - positive lookbehind: it ensurers your match starts right after $ ($ not included in the match)

[^$]{1,} - matches characters other than $; {1,} instead of * to ensure there will be no empty matches (for strings lilke "$/$")

(?=/\$) - positive lookahead: it ensures your match ends right before /$ (/$ not included in the match)

Arie
  • 5,251
  • 2
  • 33
  • 54