Solved

regular expression needed after the $

Posted on 2014-02-26
27
202 Views
Last Modified: 2014-03-18
Following data choices:

Not Needed
2hrs=$100
3hrs=$130
4hrs=$160
5hrs=$190
6hrs=$220
7hrs=$250
8hrs=$280
9hrs=$310
10hrs=$340
11hrs=$370
12hrs=$400

.net compatible regular expression

1) one of the above row is choose as the answer
2) regular expression evaluates the choice
3) need the numbers after the $ not including
4) if Not needed is the answer result is zero or nothing but no error.

thanks
0
Comment
Question by:metatonka
  • 13
  • 13
27 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
I can't provide .NET code (I use PHP), but a pattern that should work with a regex match is:
\$(\d+)

Open in new window

The number will be captured in the first group.
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
This might help shed some light on the code (I selected C#.NET), but you may actually want  VB.NET since you didn't say either way: http://www.myregextester.com/?r=ac0e0f68
0
 

Author Comment

by:metatonka
Comment Utility
when i use this
\$(\d+)
and i use this value = 9hrs=$310

i get the following:
$310;

we are almost there, how to i get the number without the "$" AND without the ";"

thank you
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
You need to get the value captured from the group. If you paste your code, I may be able to say how to do it. If you look at the code at my link above, it outputs the captured value using m.Groups[gIdx].Value

In your case, you know you want the first group, so you'd just use m.Groups[1].Value
0
 

Author Comment

by:metatonka
Comment Utility
Not code it is a regular expression elevator within a workflow program. see screen shot attached. thanks
value.jpg
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
In that case, perhaps you could try pattern:
(?<=\$)\d+

Open in new window


The (?<=\$) part of the pattern is a "positive lookbehind" which requires a $ character before the number but doesn't capture it as part of the match.
0
 

Author Comment

by:metatonka
Comment Utility
almost...
got

565;

how do we remove the ";"
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
I'm not sure why you're getting that as part of your result when it's not in the pattern. Is it perhaps a standard termination character added to the end of any captured value? Or do you think it originates from your data?
0
 

Author Comment

by:metatonka
Comment Utility
i dont know.
has not been a problem with other regular expression.
but i can run another regular expression
and replace ";" with blank
see attached screen capture
value2.jpg
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
At this point, having a play around with how the Extract option works on your software is probably the best way forward. Try changing the pattern in different ways to see when the ; character gets added to your result, and when it doesn't.

Note there is another way to match numbers other than \d - you can use [0-9] instead, in case it makes a difference. Also, instead of the + (which means match one or more, and as many as possible, of the previous character/group) you can use {1,}

\$ can be replaced with [$] too.

This means the pattern:
(?<=[$])[0-9]{1,}

Open in new window

should give the same result, but in your case where something unexpected is happening, there's a chance it may behave differently.
0
 

Author Comment

by:metatonka
Comment Utility
(?<=[$])[0-9]{1,} = 1160;
darn
the remaining ";" is still there
any suggestions?
thnaks
0
 

Author Comment

by:metatonka
Comment Utility
what works best from past questions is  .NET's regex library for this regular expression interpreter.
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Did you work out whether the ; character was from your data, or whether it was added in along the way? Does it still appear with a simpler pattern like \d ?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:metatonka
Comment Utility
No, the ";" is not a character from the data.
one of the example of data is "11hrs=$1160"

In the other regular expressions dont have this problem.

If i only use "\d" i get the following:
1;1;1;1;6;0;

I have requested other regular expressions and " .NET's regex library " is compatible with the interpreter that i am using.

any suggestions?
thanks
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
How are the other regular expressions (that work ok) different?
0
 
LVL 9

Expert Comment

by:Derek Jensen
Comment Utility
Have you tried @Terry's suggestion of using [0-9] instead of \d? It looks to me like the ; is coming from the replace portion of the regex, maybe:

Look for: \d            (or \d+ or whatever)
Replace with: $0;   (where $0 refers to the entire match of the regular expression, and would be handled automatically by the software, most likely)

Might that be possible?
0
 

Author Comment

by:metatonka
Comment Utility
Yes, i tried replacing \d with [0-9] and the ";" was there.
(?<=[$])[0-9]{1,} = 1160;
0
 

Author Comment

by:metatonka
Comment Utility
How are the other regular expressions (that work ok) different?
Since i am a newbie on regular expressions all i can tell you is that the other regular expressions are compatible to  .NET's regex library.
0
 

Author Comment

by:metatonka
Comment Utility
i really appreciate your help!
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Where the other regular expressions you've tried worked ok, was it because you weren't using the Extract option as shown in your screenshots? Perhaps adding a ; character is a standard feature of the software?
0
 

Author Comment

by:metatonka
Comment Utility
Perhaps adding a ; character is a standard feature of the software?
No other regular expression provided has had this problem. Have checked, not a standard feature of the software
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Does the provider of the software offer support in any way? It may be worth contacting them to see if they can help.
0
 

Author Comment

by:metatonka
Comment Utility
Yes, i have contacted the vendor and they suggest using regular expressions that are compatible to the  .NET's regex library.
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
Have you use the Extract option elsewhere, without the problem occurring?

Perhaps you could use trial and error to find the simplest case that causes the semi-colon to appear (eg about the simplest pattern you could use is just "1"), and then get back in touch with the software vendor. It's pretty clear that the software is not correctly working as the .NET regex library does.
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
One more thought: if you click the "Replace Text" radio button, is there anything (such as a semi-colon character) in the "Replacement text" box as shown in your screenshot? If so, perhaps that could be causing the issue even when the Extract radio button is active.
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
Comment Utility
It's also interesting you said earlier:
If i only use "\d" i get the following:
1;1;1;1;6;0;
That looks more like a Split operation than an Extract one. Are you sure you've got the correct radio button checked? If so, then there may be a software bug that is somehow causing it to do the wrong command. Sometimes, doing something like deleting the operation (or whatever a processing step in your software is called) and starting from scratch can resolve that kind of issue.

Alternatively, working around the issue by adding an extra Replacement step to replace the ; character with an empty string may be an easy way to just deal with the problem and move on.
0
 

Author Comment

by:metatonka
Comment Utility
Alternatively, working around the issue by adding an extra Replacement step to replace the ; character with an empty string may be an easy way to just deal with the problem and move on.

Unfortunately doing this causes another problem that the result is text and not a number. I need to have the original result a number so as to be able to add. This one example is a part of 15 items that the solution will be acted upon. so each workaround requires the solution to be replicated 15x.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
groovy example issue 10 64
regex to extract ip:john 17 55
Problem to start Neon 20 48
Regular expressions Exclude patter 3 8
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now