views:

197

answers:

6

Hello. I have two simple questions about regular expressions.

Having the string "$10/$50", I want to get the 50, which will always be at the end of the string. So I made: ([\d]*$)

Having the string "50c/70c" I want to get the 70, which will always be at the end of the string(i want it without the c), so I made: ([\d]*)c$

Both seem do to what I want, but I actually would like to do 2 things with it:

  • a) I'd like to put both on the same string(is it possible?). I tried with the | but it didn't seem to work.
  • b)If indeed it is possible to do a), i'd like to know if it's possible to format the text. As you can see, both for dollars and cents, I will retrieve with the regular expression the value the string shows. But while in the first case we are dealing with dollars, in the second we're dealing with cents, so I'd like to transform 50 cents into 0,5. Is it possible, or will I have to code that by myself?

Thanks!

+2  A: 

Providing these are Perl-style regexes (I don't actually know C#/.net):

(\d+(?=c$)|(?<=\$)\d+$)

Formatting the text would probably be something to do outside of the regex matching.

Wevah
Thanks. Your code does just what I was looking for. But as you make me see it, it's not possible to do point b), right? If so, I'll have to just do the 2 separate regexes, otherwise I will not know if I am dealing with the value in cents or dollars.
devoured elysium
You could take the optional c as output and do the formatting if this was set. Just add parentheses around the c too: ([\d]*)(c?)$
stiank81
+5  A: 

For matching both cases you're basically saying that the "c" is optional. Then use the "?" which means "zero or one match of the preceeding char". This should give you the following:

([\d]*)c?$

Hope that helps.

stiank81
This one should work too; mine's not very lenient at all.
Wevah
Btw; check out this site for testing your expressions: http://regexpal.com/
stiank81
+3  A: 

(a) is easy:

(\d+)c?$

(b) you can't do with regular expressions.

cletus
(b) yes, you can (in perl at least) s/.*?(\d+)(c?)$/$1*($2?0.01:1)/e but you don't want to
bjelli
+2  A: 

(\$?[\d]+)c?

The following will match both in one hit from a paragraph. Use the following tool to see with the following example text

http://gskinner.com/RegExr/

Example Text

This is the string 50c/70c $10/$50

This is the string 50c/70c $9/$50 This is the string 50c/70c $8/$50

This is the string 50c/70c $7/$50

This is the string 50c/70c $6/$50

This is the string 50c/70c $5/$50

REA_ANDREW
+2  A: 

Your existings regexes can be simplified:

([\d]*$)        ([\d]*)c$

you don't need the square backets

(\d*$)          (\d*)c$

But I'd recommend that you demand to have at least on digit in your number, so use + instead of *

(\d+$)          (\d+)c$

Now you can join the two together:

(\d+)(c?)$

I would't recommend doing the calculation inside the regex. We captured the 'c' in the second parenthesis, so we can work with this information. This is what the whole thing would look like in perl, please adapt apropriately:

if( m/(\d+)(c?)$/ ) {

   if( $2 eq 'c' ) {
       $dollars = $1/100;
   } else {
       $dollars = $1;
   }

   print "$_ are $dollars dollars\n"

}

As I commented above: the calculatino could be done in a regex / subsitution

s/.*?(\d+)(c?)$/$1*($2?0.01:1)/e

but that might bit a bit obfuscated

bjelli
A: 

Others have pointed out that (\d+)(c?)$ satisfies a).

But I see no reason why you can't follow that with substitution statements to do the formatting:

s/(\d)0c/0,$1/
s/(\d\d)c/0,$1/
s/(\d)c/0,0$1/
Beta