Jump to content

Regex help


 Share

Recommended Posts

Hello. I have a numeric string like this:

0.0.0000.0123

The last gruop of numbers can be 3 or 4. If is 3 like in this situation:

0.0.0000.123

I need to add a trailing zero after the last dot

0.0.0000.0000

At the end i need to add a dot at center of the last group:

0.0.0000.00.00

I hope everything is clear, i'm sure using group and StringRegExpReplace can be done in one line. Thanks

 

Edited by Terenz

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

Hi,

why do you need a RegEx when you are not able to understand what the RegEx-engine is doing?

Why not use the stringfunctions? Their usage should be easily done and understood by any beginner...

Or do you need someone to make your work look like "professional"?

No Regex, but understandable by beginners:

$string = "0.0.0000.123"

$pos_last_dot = StringInStr($string, ".", 0, -1)                        ;position of last dot
$right = StringRight($string, StringLen($string) - $pos_last_dot)       ;right part of string
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $right = ' & $right & @CRLF & '>Error code: ' & @error & @CRLF)
$left = StringLeft($string, $pos_last_dot)                              ;left part of string incl last dot
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $left = ' & $left & @CRLF & '>Error code: ' & @error & @CRLF)
$right = StringFormat("%04s", $right)                                   ;adding trailing zero if necessary
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $right = ' & $right & @CRLF & '>Error code: ' & @error & @CRLF)
$dotinsert = StringLeft($right, 2) & "." & StringRight($right, 2)       ;insert dot into middle of the last 4 numbers
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $dotinsert = ' & $dotinsert & @CRLF & '>Error code: ' & @error & @CRLF)
$string = $left & $dotinsert                                            ;add left part and edited right part
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $string = ' & $string & @CRLF & '>Error code: ' & @error & @CRLF)

 

Edited by AndyG
Link to comment
Share on other sites

I know how to do without regexp thanks. Is not a problem of "professional" but if i do a thing in 1 line instead of 22 imho is better.

P.S. I have used Regexp in the past but for other things and using existing example for other languages.

Nothing is so strong as gentleness. Nothing is so gentle as real strength

 

Link to comment
Share on other sites

#include <array.au3>

;Local $sInput = '0.0.0000.0123'        ; 1 Test Input
Local $sInput = '0.0.0000.123'          ; 2 Test Input
Local $aInput = StringSplit ( $sInput, "." )
If StringLen ( $aInput[ $aInput[0] ] ) = 3 Then $aInput[ $aInput[0] ] = "0" & $aInput[ $aInput[0] ]
$aInput[ $aInput[0] ] = StringRegExpReplace($aInput[ $aInput[0] ], '(\d{2})(\d{2})', '$1.$2')
Local $sNewResult = _ArrayToString ( $aInput, ".", 1 )
MsgBox (0, "New", $sNewResult )

Combination with StringSplit() and StringRegExpReplace() Unfortunately not a one-liner..

Link to comment
Share on other sites

Here is two more methods.

$TestData = "0.0.0000.123"

$Result = (StringLen(StringRegExpReplace($TestData, "^.+\.", "")) = 3) ? StringRegExpReplace($TestData, "\.(\d{1})(\d{2})$", ".0$1.$2") : StringRegExpReplace($TestData, "\.(\d{2})(\d{2})$", ".$1.$2")
ConsoleWrite($Result & @CRLF)

; Or

$Result2 = Execute(StringRegExpReplace($TestData, "(.+\.)(\d+)(\d{2})$", '((StringLen("${2}") = 2) ? "${1}${2}.$3" : "${1}0${2}.$3") '))
ConsoleWrite($Result2 & @CRLF)

 

Link to comment
Share on other sites

Something like this?

Local $s1 = "0.0.0000.123", $s2 = "0.0.0000.0123"

ConsoleWrite(_Fmt($s1) & @LF)
ConsoleWrite(_Fmt($s2) & @LF)

Func _Fmt($s)
    Return Execute(StringRegExpReplace($s, "((?:\d+\.){3})(\d+)(\d\d)", "'$1' & StringRight('0$2', 2) & '.$3'"))
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

My RegEx-solution...costs me 1/2hour and 3 cups of coffee...

$string = "0.0.0000.123"
$ret=StringRegExpReplace($string,"(?m)(.(\d+)(\d{2}))$",stringreplace(".0$2.$3",string(StringRegExp($string,"(?m)\.(\d{1})\d{2}$")),""))
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ret2 = ' & $ret & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

$string = "0.0.0000.0123"
$ret=StringRegExpReplace($string,"(?m)(.(\d+)(\d{2}))$",stringreplace(".0$2.$3",string(StringRegExp($string,"(?m)\.(\d{1})\d{2}$")),""))
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $ret2 = ' & $ret & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

//EDIT

@jchd, 

StringRight('0$2', 2)

is the short way for my

stringreplace(".0$2.$3",string(StringRegExp($string,"(?m)\.(\d{1})\d{2}$")),"")

:thumbsup:

 

Edited by AndyG
Link to comment
Share on other sites

Just for fun. With only $string = "0.0.0000.123" and a LoopCount of 10.000 Times.

@Pluto41 replacement took 0.648 Seconds to complete.
@jguinch replacement took 0.093 Seconds to complete.
@mikell replacement took 0.248 Seconds to complete.
@jchd replacement took 0.148 Seconds to complete.
@AndyG replacement took 0.168 Seconds to complete.

:)

Link to comment
Share on other sites

Many skins for this cat. The regexp doesn't have to capture the 3 groups:
 

Return Execute("'" & StringRegExpReplace($s, "(\d+)(?=\d\d$)", "' & StringRight('0$1.', 3) & "))

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...