From: Greg Lovern on 13 Jan 2010 17:06 Can a regular expression detect a repeating decimal? For example, given these input numbers, can a regex return these return values? 1.232323 -> returns "23" 1.23456789789789 -> returns "789" (ignores the "23456" before the repetitive part) 1.1371371 -> returns "137" (not 371) 1.13713713 -> returns "137" (not 713) If a regex can't do all of the work, can it do part of the work, supplemented by other code? Thanks, Greg
From: Evertjan. on 13 Jan 2010 18:06 Greg Lovern wrote on 13 jan 2010 in microsoft.public.scripting.vbscript: > Can a regular expression detect a repeating decimal? > > For example, given these input numbers, can a regex return these > return values? > > > 1.232323 -> returns "23" > 1.23456789789789 -> returns "789" (ignores the "23456" before the > repetitive part) > 1.1371371 -> returns "137" (not 371) > 1.13713713 -> returns "137" (not 713) > > If a regex can't do all of the work, can it do part of the work, > supplemented by other code? <script type='text/javascript'> function repeatingDecimal(x) { return x.replace(/^\d+\.\d*?(\d\d+)\1\d*$/,'$1'); }; alert(repeatingDecimal('1.232323')); alert(repeatingDecimal('1.23456789789789')); alert(repeatingDecimal('1.234567897897891212')); // extra alert(repeatingDecimal('1.1371371')); alert(repeatingDecimal('1.13713713')); </script> -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
From: Richard Mueller [MVP] on 13 Jan 2010 18:20 "Greg Lovern" <gregl(a)gregl.net> wrote in message news:2cd67561-323d-499e-9378-e91e395c08f2(a)j14g2000yqm.googlegroups.com... > Can a regular expression detect a repeating decimal? > > For example, given these input numbers, can a regex return these > return values? > > > 1.232323 -> returns "23" > 1.23456789789789 -> returns "789" (ignores the "23456" before the > repetitive part) > 1.1371371 -> returns "137" (not 371) > 1.13713713 -> returns "137" (not 713) > > If a regex can't do all of the work, can it do part of the work, > supplemented by other code? > > > Thanks, > > Greg The best I can do is: ([0-9]+)\1 which matches with any one or more digits repeated at least twice. However, it returns 2323, 789789, and 137137 in your examples, since that is the string that matches the pattern. I have an example VBScript program below that I use to test regular expressions, adapted from a program in Tim Hill's "Windows 2000 Script Host" shown below. It also has comments describing the syntax for patterns. You can experiment with this to see if you can do better: ============= ' RegExpMatch.vbs ' Version 1.0 ' January 8, 2010 ' VBScript program to test various regular expression patterns ' and their affect on strings. ' Based on script RegExp.vbs (Listing 7.1 on page 197) from ' "Windows 2000 Windows Script Host" by Tim Hill, ' Macmillan Technical Publishing, Copyright 1999. ' Examples of patterns: ' . any character ' .. any 2 characters ' ^abc string starts wtih abc ' red$ string ends with red ' [0-9] a digit ' [0-9]* 0 or more digits ' [0-9]+ 1 or more digits ' [0-9]? 0 or 1 digit ' [0-9a-zA-Z] alphanumeric character ' [^0-9] not a digit ' [a-zA-Z][a-zA-Z_0-9]* valid variable name ' a{3} aaa ' a{3,} at least three a characters ' a{3,5} 3 to 5 a's ' [0-9]{4} 4 digits ' \d any digit, same as [0-9] ' \D any non-digit, same as [^0-9] ' \w any word character, same as [0-9a-zA-Z_] ' \W any non-word character ' \b word boundary ' \B non-word boundary ' \s whitespace character ' \S non-whitespace character ' \f form-feed character ' \n new-line character ' \r carriage-return character ' \v vertical tab character ' \onn octal code nn ' \xnn hexadecimal code nn ' \n (n a digit) repeat previous sub expression (in parentheses) n times ' (..)\1 any two characters repeated twice, like abab ' ab|ac ab or ac ' ^ac|ac$ ab at start or ac at end of string ' ^(ab|ac)$ either string ab or ac ' [^=]+=.* command line arguments ' ([a-zA-Z]\w*)=(.*) command line arguments ' (\s*)([a-zA-Z]\w*)(\s*)=(.*) command line arguments, with whitespace ' (\s*)/([a-zA-Z]\w*)((=|:)(.*)|) command line switch ' ^(.*?)\1+$ any repeating pattern in a string Option Explicit Dim objRE, objMatches, objMatch, strPattern, strSearchString Select Case Wscript.Arguments.Count Case 1 strPattern = Wscript.Arguments(0) If (strPattern = "/?") _ Or (strPattern = "-?") _ Or (strPattern = "?") _ Or (strPattern = "/H") _ Or (strPattern = "/h") _ Or (strPattern = "-H") _ Or (strPattern = "-h") _ Or (strPattern = "/help") _ Or (strPattern = "-help") Then Call Syntax() Wscript.Quit End If Case 2 strPattern = Wscript.Arguments(0) strSearchString = Wscript.Arguments(1) Case Else Wscript.Echo "Wrong number of arguments" Call Syntax() Wscript.Quit End Select Wscript.Echo "Pattern: """ & strPattern & """" Wscript.Echo "String: """ & strSearchString & """" Set objRE = New RegExp objRE.Pattern = strPattern objRE.Global = True Set objMatches = objRE.Execute(strSearchString) Wscript.Echo "Number of matches: " & objMatches.Count For Each objMatch In objMatches Wscript.Echo "Match: " & objMatch.Value & " at: " _ & CStr(objMatch.FirstIndex + 1) & " (" & objMatch.Length & ")" Next Sub Syntax() Wscript.Echo "Syntax:" Wscript.Echo " cscript RegExpMatch.vbs <pattern> <string>" Wscript.Echo "where:" Wscript.Echo " <pattern> is a regular expression pattern" Wscript.Echo " <string> is a string to test against the pattern" Wscript.Echo "For example:" Wscript.Echo " cscript RegExpMatch.vbs ""[A-Za-z0-9\+/]+[=]?[=]?"" ""VGhpcyBpcyBhIHRlc3Q=""" End Sub -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net --
|
Pages: 1 Prev: [ann] google code search Next: Copying files from virtualstore folder with script??? |