rmmarsh
asked on
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...)
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...)
If you want the "dot" you should mask it with a leading \.
ASKER
I tried rhs: \"(d*\.d*) and it said Invalid Escape Sequence (it's in a C# program).
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".
...actually, both "\d"s.
In other words:
d - literal "d"
\d - any digit
d - literal "d"
\d - any digit
ASKER
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"
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"
ASKER
Oops... I meant the \d and the ]. [dot]
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
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
ASKER
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 \.)
Regex regex = new Regex("rhs: \"(\d*\.\d*)");
It doesn't like any of the escape sequences ( \d's or \.)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Downloaded and ran Expresso... nice program! However, when running the expression, there was no match against my data...
expresso.png
expresso.png
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.
Note the difference in the "Regular Expression" pane of my screenshot and yours.
ASKER
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.
Thank you so very much for helping me out... this now works like a champ! I really, really appreciate your time.
NP. That's what we're here for! Glad you got it sorted out = )