Micro Focus QTP (UFT) Forums
how to get only numeric values from text - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: how to get only numeric values from text (/Thread-how-to-get-only-numeric-values-from-text)

Pages: 1 2


how to get only numeric values from text - sravsand - 02-24-2011

There is a message on a webpage like Rs.4500.00/- (here there is no space between Rs and value and /- and it is a dynamic value, it may displays big number like RS340067.23/-)

How to retrieve only value without Rs. and /-


Thanks inadvance

Sandeep


RE: how to get only numeric values from text - PrabhatN - 02-24-2011

Hi Sandeep,

Try the following code snippet:

Store the value (Rs.4500.00/- or anything) in a variable, say myVar

Quote:myVar = Split(myVar,"Rs.")
myVar1 = myVar(1) \\ it will give you 4500.00/-
myVar1 = Split(myVar1,"/-")
myVar2 = myVar1(0) \\ it will give you only 4500.00
In this way you are removing Rs. and /- from any value and it will work for all values provided the format doesn't change




RE: how to get only numeric values from text - basanth27 - 02-25-2011

Same stuff in a better way,
Numric = Split(Split(myVar,"Rs.")(1),"/-")(0)


RE: how to get only numeric values from text - nanthini222 - 03-09-2011

Instead of using Split, you can also use Replace function.

Eg.
Code:
myVar="Rs.4500/-"
myVar=Replace(myVar,"Rs.","")
myVar=Replace(myVar,"/-","")
msgbox myVar
You can get 4500, by replacing unwanted characters by empty string "".


RE: how to get only numeric values from text - basanth27 - 03-09-2011

Depends on where you want to use. In the above scenario replace is cumbersome because you want to replace multiple values. while split would simply tear the rest and give you what you need at one shot.Moreover, when you split you can reuse the other part( which would be in the array) while replace, just replaces the data.


RE: how to get only numeric values from text - tdevick - 03-10-2011

you could also use regular expressions to represent your string. The regular expression below says to look for the string "RS", followed by a decimal characters (.), followed by zero or more numeric digits followed by zero or more decimal characters, followed by zero or more digits. The parentheses enclose the part of the regular expression that you want to keep (it stores it in Matches(0).SubMatches(0) in this example). The resulting string is everything but "RS" at the beginning of the string and the trailing "/-" string.


Code:
Dim regEx, Matches, returnString   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = "RS\.*(\d+\.*\d*)/-" ' Set pattern
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.

    strng="Rs.4500.00/-"
    'strng="RS340067.23/-"

   Set Matches = regEx.Execute(strng)   ' Execute search.
   If Matches.Count=1 Then
       If Matches(0).SubMatches.count=1 Then
       End If
       returnString=Matches(0).Submatches(0)
   End If

msgbox returnString

set regEx=nothing



RE: how to get only numeric values from text - basanth27 - 03-11-2011

The lines of code matter a big deal for a efficient execution.


RE: how to get only numeric values from text - tdevick - 03-11-2011

Lines of code are not the only measure of a solution. Accuracy, flexibility, ease of understanding are important as well. Regular expressions are great for dealing with ambiguity in data. Regular expressions are quite flexible and can match many variations on a data "theme". However, regular expressions often do not do as well in the "ease of understanding" area as other solutions, and, as you pointed out, they may not be as efficient as other solutions.

We run our test sets overnight, unattended. For us, speed of execution is secondary. What does the poster plan to do with the data once it is parsed? Write it to another file? That is much, much slower than the regular expression and so the regular expression is a smaller percentage of the total execution time.

I am not arguing that a regular expression is the best or only solution here, but it is a good solution if the data format is varied as the examples suggest. If the data is NOT as varied as the examples suggest (i.e., there are typos in the example), then a regular expression would be overkill - like bringing in the USS Enterprise to kill a mouse.

It does provide more information to the poster about ways to tackle the problem and, hopefully, expands the poster's knowledge of QTP a little.


RE: how to get only numeric values from text - basanth27 - 03-12-2011

Knowledge shating wise good thought, though precision in answer is of utmost concern. The above problem is simply achieved by split a one liner then a whole junk of regularly expressioned syntax. Not necessary. Maybe helpful for a different post.
Oh.. I almost forgot, automation was brought forth to cut down the manual execution speed and if you are running scripts overnight not being bothered about the speed then it is time to think what you can improve. Not every company would want a setup like yours.


RE: how to get only numeric values from text - nanthini222 - 03-21-2011

Hi Friends,

See.. Forum is basically for sharing knowledge.. People want to get different kind of answers, to improve themselves. I learned from regular expression post (Thanks for that, really a good work), as i dont have much exposure to it.

We all are already aware of more LOC need more execution time, there is nothing to say about this explicitly.

Instead of climbing to the single solution, be prepare to experiment different solutions. Thats a good way of learning..