Jump to content

StringRegExpReplace - back-references


Zedna
 Share

Recommended Posts

$text_in = _ 
'some text1 12/31/2009 01:02:03 some text2' & @CRLF & _
'some text3 02/28/2009 11:22:33 some text4'

; change date format from mm/dd/yyyy to dd.mm.yyyy
$text_out = StringRegExpReplace($text_in, '\x20(\d{2})/(\d{2})/(\d{4})\x20', ' $2.$1.$3 ')

MsgBox(0, "RegExp Replace Test - back-references", 'OLD:' & @CRLF & $text_in & @CRLF & @CRLF & 'NEW:' & @CRLF & $text_out )

Output:

OLD:

some text1 12/31/2009 01:02:03 some text2

some text3 02/28/2009 11:22:33 some text4

NEW:

some text1 31.12.2009 01:02:03 some text2

some text3 28.02.2009 11:22:33 some text4

; change date format from mm/dd/yyyy to dd.mm.yyyy
$out = StringRegExpReplace('12/31/2009', '(\d{2})/(\d{2})/(\d{4})', '$2.$1.$3')
MsgBox(0, "RegExp back-references", $out)

output:

31.12.2009

EDIT: fixed topic corruption after forum upgrade Edited by Zedna
Link to comment
Share on other sites

Zedna,

While I know how to do this myself, I think its good for the community to provide examples. In fact, I think it'd be a great idea to create a sticky thread full of examples. This would help stop the endless 'How do I do such-and-such using Regular Expressions' questions.

-Ascend4nt

Edited by ascendant
Link to comment
Share on other sites

As I said I'm absolute RegExp noob so for me this my regexp pattern was like miracle when I discovered right way to do it :-)

I started this topic as helper for other regexp noobs like me and also as reference for this feature request

http://www.autoitscript.com/trac/autoit/ticket/1017

I think it's good idea to collect such usefull/explanatory RegExp examples also with comments here

and maybe also links to good sites.

But I think examples are much better than huge documentation here

because I read some regexp documentation already but without examples it's difficult to understand it.

So feel free to post all that here and we will see how it evolves ...

Or say your opinion for this idea (collect regexp examples here that way) ...

EDIT: I can rename title/description of this topic if it evolve to collection of regexp examples

Edited by Zedna
Link to comment
Share on other sites

@Zedna

It is very gentle of you to start and dedicate this thread to the RegExpr.

If you search the forum you will see that is has been done before.

I mean people who created tools to help to get the syntax right.

Like over here :

RegExpr Guide

More RegExpr

Some Tools :

StrRegExp Tester

RegExp Tester

RegExp Tool

And some external resources :

PCR Pattern

REgExpr Reference

There is always new area's to discover in AU3.

That"s what make it fascinating :D

See you around,

Regards,

ptrex

Edited by ptrex
Link to comment
Share on other sites

Thanks for the links ptrex.

Here's the wikipedia page for regular expressions:

http://en.wikipedia.org/wiki/Regular_expression

Regular expression examples

http://en.wikipedia.org/wiki/Regular_expression_examples

I searched forum but not for documentation but for examples.

I still think it would be good to have somewhere some educative RegExp examples

demonstrating working with StringRegExp() and StringRegExpReplace()

Edited by Zedna
Link to comment
Share on other sites

#545172

http://www.autoitscript.com/forum/index.ph...mp;#entry534828

I have posted all of these at some point.

$original = "20080613104425.484375-240"
;Convert WMI date to YYYY/MM/DD HH:MM:SS
$new = StringRegExpReplace($original, "\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:.*)","$1/$2/$3 $4:$5:$6")
debug($new)

;Convert WMI date to MM/DD/YYYY HH:MM:SS
$new = StringRegExpReplace($original, "\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:.*)","$2/$3/$1 $4:$5:$6") 
debug($new)

;Convert YYYYMMDD.RRR to MM/DD/YYYY Rev. RRR
$original = "20080811.016"
$new = StringRegExpReplace($original, "\A(\d{4})(\d{2})(\d{2})\.(\d{3})","$2/$3/$1 Rev. $4") 
debug($new)

;Convert YYYY/MM/DD to MM/DD/YYYY
$original = "1980/04/30"
$new = StringRegExpReplace($original, "\A(\d*)/(\d*)/(\d*)","$2/$3/$1")
MsgBox(0,"","Before: " & $original & @CRLF & "After: " & $new)
debug($new)

;Convert MM/DD/YYYY to YYYY/MM/DD
$original = "04/30/1980"
$new = StringRegExpReplace($original, "\A(\d*)/(\d*)/(\d*)","$3/$1/$2")
MsgBox(0,"","Before: " & $original & @CRLF & "After: " & $new)
debug($new)

;Convert YYYYMMDDHHMMSS to YYYY-MM-DD HH:MM:SS
$original = "20090415153030"
$new = StringRegExpReplace($original, "\A(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(?:.*)","$1-$2-$3 $4:$5:$6")
debug($new)

;Phone number format
$objRegEx = ObjCreate("VBScript.RegExp")

$objRegEx.Global = 1
$objRegEx.Pattern = "(\d{3})-(\d{3})-(\d{4})"

$strSearchString = "555-123-4567"
$strNewString = $objRegEx.Replace($strSearchString, "($1) $2-$3")
debug($strNewString)

;Phone number format
$test = StringRegExpReplace($strSearchString, "(\d{3})-(\d{3})-(\d{4})", "($1) $2-$3")
debug($test)

Func debug($var)
    If IsArray($var) Then
        For $X = 0 to Ubound($var)-1
            ConsoleWrite("[" & $X & "]: " & $var[$X] & @CRLF)
        Next
    Else
        ConsoleWrite($var)
    EndIf
    ConsoleWrite(@CRLF)
EndFunc
Edited by weaponx
Link to comment
Share on other sites

  • 3 years later...

Maybe a bit sharper:

ConsoleWrite(StripLeadingZero('I am 001230045600 minutes old, like 0058.0184 years and 0.012345 seconds') & @LF)

Func StripLeadingZero($s)
    Return StringRegExpReplace($s, "\b(0+)(\d[\d.]*)", "$2")
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

Anchoring to A is not very useful in my view, but even then lone zeroes are significant:

ConsoleWrite(StripLeadingZero('A 0-day exploit. I am 001230045600 minutes old, like 0058.0184 years and 0.012345 seconds') & @LF)
ConsoleWrite(StripLeadingZero2('A 0-day exploit. I am 001230045600 minutes old, like 0058.0184 years and 0.012345 seconds') & @LF)

Func StripLeadingZero2($s)
    Return StringRegExpReplace($s, "\b0*", "")
EndFunc

Func StripLeadingZero($s)
    Return StringRegExpReplace($s, "\b(0+)(\d[\d.]*)", "$2")
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

Of course there is no universal silver bullet for such a task. I was just exposing mode options that I thought could be used in some real-life situations.

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...