• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

What's wrong with this RegEx?

This is my data:  {lhs: "1.00 U.S. dollar",rhs: "0.649941505 British pounds",error: "",icc: true}

This is the RegEx expression I was given, which doesn't work...   "rhs: \"(d*.d*)"  (I want to get the amount after "rhs")

What should it be?  (I know about a thimble-full about Regular Expressions, so I would appreciate someone helping me on this one...)

0
rmmarsh
Asked:
rmmarsh
  • 8
  • 6
2 Solutions
 
erictronicCommented:
If you want the "dot" you should mask it with a leading \.
0
 
rmmarshAuthor Commented:
I tried rhs: \"(d*\.d*) and it said Invalid Escape Sequence (it's in a C# program).
0
 
käµfm³d 👽Commented:
If you want the "dot" you should mask it with a leading \.
You don't really have to escape the dot, since dot means "match any character," but generally, you would escape the dot to give clarity to the meaning of your regex.

You seem to have forgotten the leading slash on the first "\d".
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
käµfm³d 👽Commented:
...actually, both "\d"s.
0
 
käµfm³d 👽Commented:
In other words:

d   -  literal "d"
\d  -  any digit
0
 
rmmarshAuthor Commented:
didn't forget... didn't know about it!  :D

This is what I have now, with plenty of errors...

                Regex regex = new Regex("rhs: \"(\d*\.d*)");

It's complaining about both of the \d as being "unrecognized escape sequence"
0
 
rmmarshAuthor Commented:
Oops... I meant the \d and the ]. [dot]
0
 
käµfm³d 👽Commented:
P.S.

Two things:

* I see that you later noted that this was for C#. Always be sure to let us know which language or text editor you are using to execute your patterns. Different languages and different text editors use various syntaxes. "\d" might represent "any digit" in one language whereas "[[:digit:]]" might be used to mean the same thing in a different language/editor.

* You might find Expresso a useful utility in your regex arsenal. Loading up your pattern into the utility, you would quickly have seen the issue with your pattern:
untitled.PNG
0
 
rmmarshAuthor Commented:
This is what I have now...

                Regex regex = new Regex("rhs: \"(\d*\.\d*)");

It doesn't like any of the escape sequences ( \d's or \.)
0
 
käµfm³d 👽Commented:
It's complaining about both of the \d as being "unrecognized escape sequence"
In C#, string literals that have backslashes in them need to either have the backslashes escaped or you need to prefix the string with the "@" symbol.

For example:

Regex regex = new Regex("rhs: \"(\\d*\\.d*)");

Open in new window


vs

Regex regex = new Regex(@"rhs: ""(\d*\.d*)");

Open in new window

0
 
käµfm³d 👽Commented:
P.S.

In the first code example, I didn't escape the backslash before the quotation mark because that backslash is escaping the quote so C# doesn't think that that quote signifies the end of string. In the @ version, the double-double-quote accomplishes the same thing, but you use an extra double-quote because the @ causes any backslashes to be treated as normal characters (i.e. they no longer escape anything--except for newlines and a couple of other whitespace characters, I believe).
0
 
rmmarshAuthor Commented:
Downloaded and ran Expresso... nice program!  However, when running the expression, there was no match against my data...
expresso.png
0
 
käµfm³d 👽Commented:
Remove the start and end quotes from your regex. It looks like you copy/pasted directly from your C# code, which is fine, but you need to leave out the quotation marks that denote a string literal in C#.

Note the difference in the "Regular Expression" pane of my screenshot and yours.
0
 
rmmarshAuthor Commented:
Argggghhhh!  I hate it when I forget something like that!

Thank you so very much for helping me out... this now works like a champ!  I really, really  appreciate your time.
0
 
käµfm³d 👽Commented:
NP. That's what we're here for! Glad you got it sorted out    = )
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now