![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Need help to split a string into text and numeric values !! Hi, I need a way to extract the numbers from units. I read each value into a variable strline and then need to split the units and numbers into 2 seperate variables var1 and var2. Here's the input.txt file and part of the code. Thanks, SA Part of the code: ============ strline=objFileR.readLine Do until objFileR.AtEndofStream if RegExprTest("K|M|MHz|GHz", strline) then 'var1 = gets numbers 'var2 = gets units Input.txt: ====== 200K 750K 1M 915MHz 433.92MHz 802.11GHz 2.4GHz 2.45GHz |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Need help to split a string into text and numeric values !! <karsagarwal@xxxxxx> wrote in message news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx Quote: > Hi, > > I need a way to extract the numbers from units. I read each value into > a variable strline and then need to split the units and numbers into 2 > seperate variables var1 and var2. Here's the input.txt file and part > of the code. > > Thanks, > SA > > Part of the code: > ============ > strline=objFileR.readLine > > Do until objFileR.AtEndofStream > if RegExprTest("K|M|MHz|GHz", strline) then > 'var1 = gets numbers > 'var2 = gets units > > > Input.txt: > ====== > 200K > 750K > 1M > 915MHz > 433.92MHz > 802.11GHz > 2.4GHz > 2.45GHz captures "()" in the pattern and the use of the SubMatches array. Option Explicit Dim rgx: Set rgx = New RegExp rgx.Pattern = "([\d\.]+)(.+)" Dim oMatch For Each oMatch in rgx.Execute("433.92MHz") MsgBox oMatch.SubMatches(0) & " : " & oMatch.SubMatches(1) Next -- Anthony Jones - MVP ASP/ASP.NET |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Need help to split a string into text and numeric values !! On Sep 2, 2:17*pm, "Anthony Jones" <AnthonyWJo...@xxxxxx> wrote: Quote: > <karsagar...@xxxxxx> wrote in message > > news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx > > > > > Quote: > > Hi, Quote: > > I need a way to extract the numbers from units. I read each value into > > a variable strline and then need to split the units and numbers into 2 > > seperate variables var1 and var2. Here's the input.txt file and part > > of the code. Quote: > > Thanks, > > SA Quote: > > Part of the code: > > ============ > > strline=objFileR.readLine Quote: > > Do until objFileR.AtEndofStream > > if RegExprTest("K|M|MHz|GHz", strline) then > > * * * * * * * * * * 'var1 = gets numbers > > * * * * * * * * * * 'var2 = gets units Quote: > > Input.txt: > > ====== > > 200K > > 750K > > 1M > > 915MHz > > 433.92MHz > > 802.11GHz > > 2.4GHz > > 2.45GHz > Examine the following script. *The key things to note are the use of > captures "()" in the pattern and the use of the SubMatches array. > > Option Explicit > > Dim rgx: Set rgx = New RegExp > > rgx.Pattern = "([\d\.]+)(.+)" > > Dim oMatch > For Each oMatch in rgx.Execute("433.92MHz") > *MsgBox oMatch.SubMatches(0) & *" : " & oMatch.SubMatches(1) > Next > > -- > Anthony Jones - MVP ASP/ASP.NET- Hide quoted text - > > - Show quoted text - t ofigure it out. Thanks, SA |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Need help to split a string into text and numeric values !! On Sep 2, 2:49*pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> wrote: Quote: > On Sep 2, 2:17*pm, "Anthony Jones" <AnthonyWJo...@xxxxxx> > wrote: > > > > > Quote: > > <karsagar...@xxxxxx> wrote in message Quote: > >news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx Quote: Quote: > > > Hi, Quote: Quote: > > > I need a way to extract the numbers from units. I read each value into > > > a variable strline and then need to split the units and numbers into 2 > > > seperate variables var1 and var2. Here's the input.txt file and part > > > of the code. Quote: Quote: > > > Thanks, > > > SA Quote: Quote: > > > Part of the code: > > > ============ > > > strline=objFileR.readLine Quote: Quote: > > > Do until objFileR.AtEndofStream > > > if RegExprTest("K|M|MHz|GHz", strline) then > > > * * * * * * * * * * 'var1 = gets numbers > > > * * * * * * * * * * 'var2 = gets units Quote: Quote: > > > Input.txt: > > > ====== > > > 200K > > > 750K > > > 1M > > > 915MHz > > > 433.92MHz > > > 802.11GHz > > > 2.4GHz > > > 2.45GHz Quote: > > Examine the following script. *The key things to note are the use of > > captures "()" in the pattern and the use of the SubMatches array. Quote: > > Option Explicit Quote: > > Dim rgx: Set rgx = New RegExp Quote: > > rgx.Pattern = "([\d\.]+)(.+)" Quote: > > Dim oMatch > > For Each oMatch in rgx.Execute("433.92MHz") > > *MsgBox oMatch.SubMatches(0) & *" : " & oMatch.SubMatches(1) > > Next Quote: > > -- > > Anthony Jones - MVP ASP/ASP.NET- Hide quoted text - Quote: > > - Show quoted text - > The link does not work. CAn you please explain in detail. I can't seem > t ofigure it out. > > Thanks, > SA- Hide quoted text - > > - Show quoted text - A very big thank you for your help. It works like magic. Could you please explain the pattern search in detail. Grateful, SA |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Need help to split a string into text and numeric values !! On Sep 2, 3:23*pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> wrote: Quote: > On Sep 2, 2:49*pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> > wrote: > > > > > Quote: > > On Sep 2, 2:17*pm, "Anthony Jones" <AnthonyWJo...@xxxxxx> > > wrote: Quote: Quote: > > > <karsagar...@xxxxxx> wrote in message Quote: Quote: > > >news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx Quote: Quote: > > > > Hi, Quote: Quote: > > > > I need a way to extract the numbers from units. I read each value into > > > > a variable strline and then need to split the units and numbers into 2 > > > > seperate variables var1 and var2. Here's the input.txt file and part > > > > of the code. Quote: Quote: > > > > Thanks, > > > > SA Quote: Quote: > > > > Part of the code: > > > > ============ > > > > strline=objFileR.readLine Quote: Quote: > > > > Do until objFileR.AtEndofStream > > > > if RegExprTest("K|M|MHz|GHz", strline) then > > > > * * * * * * * * * * 'var1 = gets numbers > > > > * * * * * * * * * * 'var2 = gets units Quote: Quote: > > > > Input.txt: > > > > ====== > > > > 200K > > > > 750K > > > > 1M > > > > 915MHz > > > > 433.92MHz > > > > 802.11GHz > > > > 2.4GHz > > > > 2.45GHz Quote: Quote: > > > Examine the following script. *The key things to note are the use of > > > captures "()" in the pattern and the use of the SubMatches array. Quote: Quote: > > > Option Explicit Quote: Quote: > > > Dim rgx: Set rgx = New RegExp Quote: Quote: > > > rgx.Pattern = "([\d\.]+)(.+)" Quote: Quote: > > > Dim oMatch > > > For Each oMatch in rgx.Execute("433.92MHz") > > > *MsgBox oMatch.SubMatches(0) & *" : " & oMatch.SubMatches(1) > > > Next Quote: Quote: > > > -- > > > Anthony Jones - MVP ASP/ASP.NET- Hide quoted text - Quote: Quote: > > > - Show quoted text - Quote: > > The link does not work. CAn you please explain in detail. I can't seem > > t ofigure it out. Quote: > > Thanks, > > SA- Hide quoted text - Quote: > > - Show quoted text - > *Anthony: > > A very big thank you for your help. It works like magic. Could you > please explain the pattern search in detail. > > Grateful, > SA- Hide quoted text - > > - Show quoted text - rgx.Pattern = "([\d\.]+)(.+)" This does not work with a "few changes" to my input.txt. So if I have just a 1, 3.65 or MBRS130LT3 with no units (K ,MHz etc) I get the following output. Output Sample ========== 200K = 200 : K (Correct) 1 = blank should be 1 3.65 = 3.6 : 5. should be 3.65 MBRS130LT3 = 130 : LT should be MBRS130LT3 Input.txt: ====== 200K 750K 1 1M 915MHz 433.92MHz 802.11GHz MBRS130LT3 3.65 2.4GHz 2.45GHz I need to parse data with % and numbers example: |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Need help to split a string into text and numeric values !! <karsagarwal@xxxxxx> wrote in message news:25becee5-af99-423e-a921-9476e7ea6b3a@xxxxxx On Sep 2, 3:23 pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> wrote: Quote: > On Sep 2, 2:49 pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> > wrote: > > > > > Quote: > > On Sep 2, 2:17 pm, "Anthony Jones" <AnthonyWJo...@xxxxxx> > > wrote: Quote: Quote: > > > <karsagar...@xxxxxx> wrote in message Quote: Quote: > > >news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx Quote: Quote: > > > > Hi, Quote: Quote: > > > > I need a way to extract the numbers from units. I read each value > > > > into > > > > a variable strline and then need to split the units and numbers into > > > > 2 > > > > seperate variables var1 and var2. Here's the input.txt file and part > > > > of the code. Quote: Quote: > > > > Thanks, > > > > SA Quote: Quote: > > > > Part of the code: > > > > ============ > > > > strline=objFileR.readLine Quote: Quote: > > > > Do until objFileR.AtEndofStream > > > > if RegExprTest("K|M|MHz|GHz", strline) then > > > > 'var1 = gets numbers > > > > 'var2 = gets units Quote: Quote: > > > > Input.txt: > > > > ====== > > > > 200K > > > > 750K > > > > 1M > > > > 915MHz > > > > 433.92MHz > > > > 802.11GHz > > > > 2.4GHz > > > > 2.45GHz Quote: Quote: > > > Examine the following script. The key things to note are the use of > > > captures "()" in the pattern and the use of the SubMatches array. Quote: Quote: > > > Option Explicit Quote: Quote: > > > Dim rgx: Set rgx = New RegExp Quote: Quote: > > > rgx.Pattern = "([\d\.]+)(.+)" Quote: Quote: > > > Dim oMatch > > > For Each oMatch in rgx.Execute("433.92MHz") > > > MsgBox oMatch.SubMatches(0) & " : " & oMatch.SubMatches(1) > > > Next Quote: Quote: > > > -- > > > Anthony Jones - MVP ASP/ASP.NET- Hide quoted text - Quote: Quote: > > > - Show quoted text - Quote: > > The link does not work. CAn you please explain in detail. I can't seem > > t ofigure it out. Quote: > > Thanks, > > SA- Hide quoted text - Quote: > > - Show quoted text - > Anthony: > > A very big thank you for your help. It works like magic. Could you > please explain the pattern search in detail. > > Grateful, > SA- Hide quoted text - > > - Show quoted text - rgx.Pattern = "([\d\.]+)(.+)" This does not work with a "few changes" to my input.txt. ===> I'm no regular expression guru, but it seems to me that Anthony developed an expression that was based on an assumption about the nature of the data, specifically having numeric characters followed by others the first of which is non-numeric. Before you can arrive at a scripting solution, you need to fully define what the scope of the problem is. If your input might contain blank lines, binary data, data with the sequence in reverse order (i.e. $15.25), random input, numeric only, non-numeric only, or with numbers spelled out (two KG), this needs to be spelled out *before* any code is written. /Al So if I have just a 1, 3.65 or MBRS130LT3 with no units (K ,MHz etc) I get the following output. Output Sample ========== 200K = 200 : K (Correct) 1 = blank should be 1 3.65 = 3.6 : 5. should be 3.65 MBRS130LT3 = 130 : LT should be MBRS130LT3 Input.txt: ====== 200K 750K 1 1M 915MHz 433.92MHz 802.11GHz MBRS130LT3 3.65 2.4GHz 2.45GHz I need to parse data with % and numbers example: |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Need help to split a string into text and numeric values !! Al Dunbar wrote: Quote: > <karsagarwal@xxxxxx> wrote in message Quote: > ===> I'm no regular expression guru, but it seems to me that Anthony > developed an expression that was based on an assumption about the nature of > the data, specifically having numeric characters followed by others the > first of which is non-numeric. Before you can arrive at a scripting > solution, you need to fully define what the scope of the problem is. If your > input might contain blank lines, binary data, data with the sequence in > reverse order (i.e. $15.25), random input, numeric only, non-numeric only, > or with numbers spelled out (two KG), this needs to be spelled out *before* > any code is written. > > /Al > it's hard to find your reply in the wealth of unnecessarily reposted parts. Also your browser doesn't seem to indent the old messages. I know, even OE can be trimmed to apply to usenet standards ;-) With input data from OP and this changed Regexp regExpr.Pattern = "^([0-9\.]*)([^0-9\.]*.*)" I get this output: ==screen=copy=============================================================== 14:20:55 C:\Test\vbs>RegExp2.vbs (200K) 200 : K (750K) 750 : K (1) 1 : (1M) 1 : M (915MHz) 915 : MHz (433.92MHz) 433.92 : MHz (802.11GHz) 802.11 : GHz (MBRS130LT3) : MBRS130LT3 (3.65) 3.65 : (2.4GHz) 2.4 : GHz (2.45GHz) 2.45 : GHz==screen=copy=============================================================== The problem in the first pattern was the + which requires at least one occurence of the tagged part. As either could be absent and the definition was changed underway it couldn't work. The new pattern takes all digits and dots anchored from start(^) upto any number of non digit or dot characters (negated class with ^ inside brackets) followed by the remainder of the line =.* you could also add a $sign but this is redundant . Here my whole RegExp2.vbs________________________________________________ Option Explicit Dim fiiLename, fso, txs, text, regExpr, oMatch, oMatches, retVal Const ForReading = 1 fiiLename = "C:\Test\Input.txt" set fso = createobject("scripting.filesystemobject") set txs = fso.opentextfile(fiiLename, ForReading, False) Set regExpr = New RegExp regExpr.Pattern = "^([0-9\.]*)([^0-9\.]*.*)" Do While txs.AtEndOfLine <> True text = txs.readline Set oMatches = regExpr.Execute(text) For Each oMatch in oMatches RetVal = oMatch.SubMatches(0) & vbTab & ": " & oMatch.SubMatches(1) Next wscript.echo "(" & LJust(text & ")", 15 ) & retVal Loop Function LJust(Str, iLen) If Len (Str) < iLen then LJust=Left(Str & Space(iLen), iLen) Else LJust=Str End If end Function -- HTH Matthias |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Need help to split a string into text and numeric values !! <karsagarwal@xxxxxx> wrote in message news:25becee5-af99-423e-a921-9476e7ea6b3a@xxxxxx On Sep 2, 3:23 pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> wrote: Quote: > On Sep 2, 2:49 pm, "karsagar...@xxxxxx" <karsagar...@xxxxxx> > wrote: > > > > > Quote: > > On Sep 2, 2:17 pm, "Anthony Jones" <AnthonyWJo...@xxxxxx> > > wrote: Quote: Quote: > > > <karsagar...@xxxxxx> wrote in message Quote: Quote: > > >news:b3562793-eba7-432d-ad0b-5db1d9a85e05@xxxxxx Quote: Quote: > > > > Hi, Quote: Quote: > > > > I need a way to extract the numbers from units. I read each value > > > > into > > > > a variable strline and then need to split the units and numbers into > > > > 2 > > > > seperate variables var1 and var2. Here's the input.txt file and part > > > > of the code. Quote: Quote: > > > > Thanks, > > > > SA Quote: Quote: > > > > Part of the code: > > > > ============ > > > > strline=objFileR.readLine Quote: Quote: > > > > Do until objFileR.AtEndofStream > > > > if RegExprTest("K|M|MHz|GHz", strline) then > > > > 'var1 = gets numbers > > > > 'var2 = gets units Quote: Quote: > > > > Input.txt: > > > > ====== > > > > 200K > > > > 750K > > > > 1M > > > > 915MHz > > > > 433.92MHz > > > > 802.11GHz > > > > 2.4GHz > > > > 2.45GHz Quote: Quote: > > > Examine the following script. The key things to note are the use of > > > captures "()" in the pattern and the use of the SubMatches array. Quote: Quote: > > > Option Explicit Quote: Quote: > > > Dim rgx: Set rgx = New RegExp Quote: Quote: > > > rgx.Pattern = "([\d\.]+)(.+)" Quote: Quote: > > > Dim oMatch > > > For Each oMatch in rgx.Execute("433.92MHz") > > > MsgBox oMatch.SubMatches(0) & " : " & oMatch.SubMatches(1) > > > Next Quote: Quote: > > > -- > > > Anthony Jones - MVP ASP/ASP.NET- Hide quoted text - Quote: Quote: > > > - Show quoted text - Quote: > > The link does not work. CAn you please explain in detail. I can't seem > > t ofigure it out. Quote: > > Thanks, > > SA- Hide quoted text - Quote: > > - Show quoted text - > Anthony: > > A very big thank you for your help. It works like magic. Could you > please explain the pattern search in detail. > > Grateful, > SA- Hide quoted text - > > - Show quoted text - rgx.Pattern = "([\d\.]+)(.+)" This does not work with a "few changes" to my input.txt. So if I have just a 1, 3.65 or MBRS130LT3 with no units (K ,MHz etc) I get the following output. Output Sample ========== 200K = 200 : K (Correct) 1 = blank should be 1 3.65 = 3.6 : 5. should be 3.65 MBRS130LT3 = 130 : LT should be MBRS130LT3 Input.txt: ====== 200K 750K 1 1M 915MHz 433.92MHz 802.11GHz MBRS130LT3 3.65 2.4GHz 2.45GHz I need to parse data with % and numbers example: -------------------------------------------------------------------------- There are many flavors of Regular Expression engines, and even if there were only one universal RE engine, regular expressions would still be a complex and confusing subject. It would be nice if there were a regular expression interpreter, to convert something like: "([\d\.]+)(.+)" into: Capture Any character in "\d\." + (one or more times) End Capture Capture . (any character) + (one or more times) End Capture or convert : "^([0-9\.]*)([^0-9\.]*.*)" into: ^ (anchor to start of string) Capture Any character in "0-9\." * (zero or more times) End Capture Capture Any character not in "0-9\." * (zero or more times) . (any character) * (zero or more times) End Capture The interpretations above are how the dot net RE engine would execute the sample regular expressions. VBScript's RE engine might interpret them differently. I have found only one free RE interpreter, called Regular Expression Workbench, which is dot net specific, but correctly interprets many VBScript regular expressions. I'm not sure if it is still available -- Microsoft rearranges (maybe scrambles is a better word) its web sites pretty often. Last year the following URL took you to the download page: http://www.gotdotnet.com/Community/U...1-4EE2729D7322 You might search for something like: Regular Expression Workbench Eric Gunnerson (EricGu@xxxxxx) -Paul Randall |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Need help to split a string into text and numeric values !! Paul Randall wrote: Quote: > The interpretations above are how the dot net RE engine would execute the > sample regular expressions. VBScript's RE engine might interpret them > differently. > the regex derivates. Also you will IMHO have to learn by experience, cause some not so obvious things like the greediness without which my regex wouldn't work. Quote: > I have found only one free RE interpreter, called Regular Expression > Workbench, which is dot net specific, but correctly interprets many VBScript > regular expressions. I'm not sure if it is still available -- Microsoft > rearranges (maybe scrambles is a better word) its web sites pretty often. > Last year the following URL took you to the download page: > http://www.gotdotnet.com/Community/U...1-4EE2729D7322 > <http://www.ultrapico.com/Resources.htm> -- HTH Matthias |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Need help to split a string into text and numeric values !! "Matthias Tacke" <Matthias@xxxxxx> wrote in message news:6i7pgiFphrqfU1@xxxxxx Quote: > Here are some other/more links: > <http://www.ultrapico.com/Resources.htm> > own regular expressions and about analyzing RE's I find on the net. -Paul Randall |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Custom format numeric values | PowerShell | |||
| Re: How to tell if a string contains a numeric value | PowerShell | |||
| How to tell if a string contains a numeric value | PowerShell | |||
| Convert a numeric string to COMP? (binary as hex) | PowerShell | |||
| convert int to numeric (formatted) string | .NET General | |||