iahngy Posted February 11, 2014 Share Posted February 11, 2014 Hi , I got a string with lines ..as below ..how can I use regex to extract a group of 3 lines that has Byron in it ? 3 lines include machine , date and line of owner name. such as Machine1 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCbyron) place holder ----------------------------------------------------------- Day Time Line Week Month February 9 - February 15 Sunday, February 09, 2014 Add Machine123 Machine1 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCbyron) place holder Machine2 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABChaha) place holder Machine3 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCbrandon) place holder Machine4 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCChris) place holder Machine5 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCbyron) place holder Machine6 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCjoe) place holder Machine7 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABCjoe) place holder Link to comment Share on other sites More sharing options...
jdelaney Posted February 11, 2014 Share Posted February 11, 2014 (edited) kind of a hack, but it works: $string = FileRead("test2.txt") $a = StringRegExp($string, "(.*[\r\n].*[\r\n]*.*\(ABC\\byron\) place holder)",3) ; For $i = 0 To UBound($a)-1 ConsoleWrite($a[$i] & @CRLF) Next or, to break up the lines within their groups (create array of arrays): $string = FileRead("test2.txt") $a = StringRegExp($string, "(.*)[\r\n](.*)[\r\n](.*\(ABC\\byron\) place holder)",4) ; For $i = 0 To UBound($a)-1 _ArrayDisplay($a[$i]) Next Edited February 11, 2014 by jdelaney IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
mikell Posted February 11, 2014 Share Posted February 11, 2014 A bit simpler $string = FileRead("1.txt") ;$a = StringRegExp($string, "(.*[\r\n].*[\r\n]*.*\(ABC\\byron\) place holder)",3) $a = StringRegExp($string, 'Machine\d+\R.+\R.+byron.+', 3) For $i = 0 To UBound($a)-1 msgbox(0,"", $a[$i]) Next Link to comment Share on other sites More sharing options...
Malkey Posted February 11, 2014 Share Posted February 11, 2014 An array free example. local $sTestString = StringRegExpReplace(FileRead(@ScriptFullPath), "(?is)^.*#cs\s*(.+)#ce.*$","\1") ;ConsoleWrite($sTestString & @LF) local $sResult = StringRegExpReplace($sTestString, "(?is).*?(\V+\v+\V+\v+\V*byron\V*\v+|$).*?","\1") ConsoleWrite($sResult & @LF) #cs Day Time Line Week Month February 9 - February 15 Sunday, February 09, 2014 Add Machine123 Machine1 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\byron) place holder Machine2 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\haha) place holder Machine3 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\brandon) place holder Machine4 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\Chris) place holder Machine5 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\byron) place holder Machine6 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\joe) place holder Machine7 2/7/2014 6:00:00 PM - 2/10/2014 8:00:00 AM (ABC\joe) place holder #ce Link to comment Share on other sites More sharing options...
iahngy Posted February 12, 2014 Author Share Posted February 12, 2014 (edited) Thank you for the quick answers to Jdelaney, Mikell and Malkey. I m very much appreciated. I tried them all out. All wonderfull to know. Malky, for the statement , local $sResult = StringRegExpReplace($sTestString, "(?is).*?(V+v+V+v+V*byronV*v+|$).*?","1") .*? means 0 or more whatever infront those 3 lines ? v+|$ means : either LF or end of string ? This method to get stuff out of string is cool to know finally. Edited February 12, 2014 by iahngy Link to comment Share on other sites More sharing options...
kylomas Posted February 12, 2014 Share Posted February 12, 2014 (edited) @Malkey - Why the "/s*" in the pattern to create your test data? This example works with or without it. edit:puctuation Edited February 12, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted February 12, 2014 Share Posted February 12, 2014 kylomas, It works indeed But it's the best way to get the text from its real beginning, without the leading spaces/newlines Link to comment Share on other sites More sharing options...
kylomas Posted February 12, 2014 Share Posted February 12, 2014 (edited) @mikell - Thanks @Malkey - been looking, on and off, for an easy way to create test data from forum examples, THANKS! Edited February 12, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
guinness Posted February 12, 2014 Share Posted February 12, 2014 @Malkey - been looking, on and off, for an easy way to create test data from forum examples, THANKS! Check the AutoIt Wrappers Wiki, I created about 6/7 functions for creating test data. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
kylomas Posted February 12, 2014 Share Posted February 12, 2014 (edited) @guinness - Thanks Edited February 12, 2014 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill 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