Jump to content
Sign in to follow this  
iahngy

how to extract certain lines out from a string

Recommended Posts

iahngy

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

Share this post


Link to post
Share on other sites
jdelaney

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

Share this post


Link to post
Share on other sites
mikell

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

Share this post


Link to post
Share on other sites
Malkey

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

Share this post


Link to post
Share on other sites
iahngy

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 by iahngy

Share this post


Link to post
Share on other sites
kylomas

@Malkey - Why the "/s*" in the pattern to create your test data?  This example works with or without it.

edit:puctuation

Edited 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

Share this post


Link to post
Share on other sites
mikell

kylomas,

It works indeed  :)

But it's the best way to get the text from its real beginning, without the leading spaces/newlines

Share this post


Link to post
Share on other sites
kylomas

@mikell - Thanks

@Malkey - been looking, on and off, for an easy way to create test data from forum examples, THANKS! 

Edited 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

Share this post


Link to post
Share on other sites
guinness

 

@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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
kylomas

@guinness - Thanks

Edited 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

Share this post


Link to post
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
Sign in to follow this  

×