Zedna Posted June 10, 2009 Share Posted June 10, 2009 (edited) $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 July 20, 2009 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Ascend4nt Posted June 10, 2009 Share Posted June 10, 2009 (edited) 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 June 10, 2009 by ascendant My contributions: Performance Counters in Windows - Measure CPU, Disk, Network etc Performance | Network Interface Info, Statistics, and Traffic | CPU Multi-Processor Usage w/o Performance Counters | Disk and Device Read/Write Statistics | Atom Table Functions | Process, Thread, & DLL Functions UDFs | Process CPU Usage Trackers | PE File Overlay Extraction | A3X Script Extract | File + Process Imports/Exports Information | Windows Desktop Dimmer Shade | Spotlight + Focus GUI - Highlight and Dim for Eyestrain Relief | CrossHairs (FullScreen) | Rubber-Band Boxes using GUI's (_GUIBox) | GUI Fun! | IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) | Magnifier (Vista+) Functions UDF | _DLLStructDisplay (Debug!) | _EnumChildWindows (controls etc) | _FileFindEx | _ClipGetHTML | _ClipPutHTML + ClipPutHyperlink | _FileGetShortcutEx | _FilePropertiesDialog | I/O Port Functions | File(s) Drag & Drop | _RunWithReducedPrivileges | _ShellExecuteWithReducedPrivileges | _WinAPI_GetSystemInfo | dotNETGetVersions | Drive(s) Power Status | _WinGetDesktopHandle | _StringParseParameters | Screensaver, Sleep, Desktop Lock Disable | Full-Screen Crash Recovery Wrappers/Modifications of others' contributions: _DOSWildcardsToPCRegEx (original code: RobSaunder's) | WinGetAltTabWinList (original: Authenticity) UDF's added support/programming to: _ExplorerWinGetSelectedItems | MIDIEx UDF (original code: eynstyne) (All personal code/wrappers centrally located at Ascend4nt's AutoIT Code) Link to comment Share on other sites More sharing options...
Zedna Posted June 10, 2009 Author Share Posted June 10, 2009 (edited) 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/1017I think it's good idea to collect such usefull/explanatory RegExp examples also with comments hereand maybe also links to good sites.But I think examples are much better than huge documentation herebecause 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 June 10, 2009 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
ptrex Posted June 11, 2009 Share Posted June 11, 2009 (edited) @ZednaIt 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 GuideMore RegExprSome Tools :StrRegExp TesterRegExp TesterRegExp ToolAnd some external resources :PCR PatternREgExpr ReferenceThere is always new area's to discover in AU3. That"s what make it fascinating See you around,Regards,ptrex Edited June 11, 2009 by ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
Zedna Posted June 11, 2009 Author Share Posted June 11, 2009 (edited) Thanks for the links ptrex.Here's the wikipedia page for regular expressions:http://en.wikipedia.org/wiki/Regular_expressionRegular expression exampleshttp://en.wikipedia.org/wiki/Regular_expression_examplesI searched forum but not for documentation but for examples.I still think it would be good to have somewhere some educative RegExp examplesdemonstrating working with StringRegExp() and StringRegExpReplace() Edited June 11, 2009 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
weaponx Posted June 11, 2009 Share Posted June 11, 2009 (edited) #545172http://www.autoitscript.com/forum/index.ph...mp;#entry534828I have posted all of these at some point.expandcollapse popup$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 June 11, 2009 by weaponx Link to comment Share on other sites More sharing options...
Zedna Posted May 26, 2013 Author Share Posted May 26, 2013 ConsoleWrite(StripLeadingZero('001230045600')) Func StripLeadingZero($s) Return StringRegExpReplace($s, "\A0*([^0]*?)", "$1") EndFunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
jchd Posted May 27, 2013 Share Posted May 27, 2013 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
AZJIO Posted May 27, 2013 Share Posted May 27, 2013 Func StripLeadingZero2($s) Return StringRegExpReplace($s, "\A0*", "") EndFunc My other projects or all Link to comment Share on other sites More sharing options...
jchd Posted May 28, 2013 Share Posted May 28, 2013 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
AZJIO Posted May 28, 2013 Share Posted May 28, 2013 Depends on the input data. This is not a universal solution. This affects the speed. My other projects or all Link to comment Share on other sites More sharing options...
jchd Posted May 28, 2013 Share Posted May 28, 2013 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now