tags:

views:

75

answers:

4

I just simply want to retrieve two substring of a string which take the following form. The first is a numerical value (there is only one) which is encased between parentheses, such as (12345) - this can be any number of digits (I haven't really kept any stats on length) but between 1 and 10 digits should cover it. The second substring takes the form of $ some numerical value + $ second numerical value such as ($100 + $5) so again how would I pull that? I have 'googled' possible solutions and failed miserably to get something to work so a little explanation might help me learn something...

I forgot to mention that the numeric value can be $100 + $5.20 or $10 + $0.20 or of the form $0.05/$0.10.

Thanks.

A: 

/\((\d+)\)/ For the first Substring
/\((\$\d+(?:\s*\+\s*\$\d+)*)\)/ for the second one

For the first Substring I actually look only to digits between "(" and ")".

The second one will look for "$" followed by digits, then for a group containing "+" and followed by "$" and digits. The last group can be repeated more than once. It will also match ($12 +$30+ $13 + $14)

Another way to do the second one is this, /\((\$\d+ \+ \$\d+)\)/. This time the "last group" from the previous regex doesn't exists. It only looks for "$" followed by digits then a space, a "+", another space, and again a "$" followed by digits.


A simple warning, I used "/" delimiters in my regexes, you have to remove them to use the regexes in C#

Colin Hebert
You have some extra `/` before and after each regex. In C#/.NET these would be interpreted as literal slashes, so they will make the regex not match.
Timwi
I gave "standard" regexes with delimiters I'll let the OP taking care of them. (But I'll add a warning)
Colin Hebert
A: 

To find a number in parentheses:

var input = "Blah (12345) blah";
var m = Regex.Match(input, @"\((\d+)\)");
if (m.Success)
    // Outputs 12345
    Console.WriteLine(m.Groups[1].Value);

For the dollar + number expression:

var input = "Blah ($100 + $5) blah";
var m = Regex.Match(input, @"\(\$(\d+)\s*\+\s*\$(\d+)\)");
if (m.Success)
{
    // Outputs 100
    Console.WriteLine(m.Groups[1].Value);
    // Outputs 5
    Console.WriteLine(m.Groups[2].Value);
}
Timwi
+1  A: 

If you want to retrieve the two substrings with only one regex you can do, depending of the order the two substrings apear within the original string :

input = "bla bla (12345) bla bla ($123 + $456) bla bla"

\((\d+)\).*?\(\$(\d+)\s*\+\s*\$(\d+)\)

or

input = "bla bla ($123 + $456) bla bla (12345) bla bla"

\(\$(\d+)\s*\+\s*\$(\d+)\).*?\((\d+)\)

If numbers length are between 1 and 10 digit long, you can replace all occurrences of \d+ by \d{1,10}

Update :

if you want to match numbers with optional decimal part you have to change \d+ by \d+(?:\.\d\d)? then the regex becomes :

\((\d+(?:\.\d\d)?)\).*?\(\$(\d+(?:\.\d\d)?)\s*\+\s*\$(\d+(?:\.\d\d)?)\)
M42
How do I modify it for decimal numbers such as $125.50? or $0.20? Thanks.
flavour404
A: 

I would suggest this expression:

(?<Group1>\(\d+\))|(?<Group2>\(\$\d+\s*\+\s*\$\d+\))

It uses an 'or' between the two groups you want to match. So it does not matter in what sequence or how often these appear in the input. Each time one of the parts of the 'or' condition matches you will get a Regex match in the resulting match collection. The names 'Group1' and 'Group2' give you easier access to what was matched: match.Groups["Group1"].Value for example

Bernd
The names “Group1” and “Group2” are easier than the numbers 1 and 2?
Timwi