Dolemite50 Posted November 21, 2009 Posted November 21, 2009 Hi, The other day I needed to add "ITEM: " to the beginning of each line in a file, which I did by reading the file to an array, adding "ITEM: " to each in iteration, and then overwriting the file with new array. I thought it might be faster to try the same with RegExpReplace on the whole file at once but I couldn't seem to get the syntax right for putting something at the start of each line. I tried every variation of the following that I could think of: \A - Match only at beginning of string. \n - Match a linefeed (@LF, chr(10)). \r - Match a carriage return (@CR, chr(13)). \v - any vertical whitespace character. ^ - beginning of string. (?m) - ^ and $ match newlines within data. Any help?
Authenticity Posted November 21, 2009 Posted November 21, 2009 If you need to increase something or work on the data: ; Thanks to Malkey for the idea, http://www.autoitscript.com/forum/index.php?showtopic=97593&st=0&p=717546&hl=stringregexpreplace&fromsearch=1&#entry717546 Local $sFileText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." & @CRLF & _ "Curabitur pulvinar malesuada metus, in sollicitudin libero pulvinar et." & @CRLF & _ "Nullam nisi lectus, ultrices ut fringilla in, consequat a dolor." & @CRLF & _ "Morbi pulvinar commodo fermentum. Nunc libero quam, elementum nec elementum eget, bibendum sed orci." & @CRLF & _ "Aliquam iaculis augue sed leo vehicula tincidunt." & @CRLF & _ "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;" & @CRLF & _ "Fusce ut eros quam. Phasellus tempor odio lobortis eros ultrices varius." & @CRLF & _ "Nullam aliquet malesuada nibh, id molestie purus sagittis interdum." & @CRLF & _ "Mauris quam nisl, ultrices sit amet scelerisque auctor, scelerisque eget dolor." Local $sRegExp1 = "'" & StringRegExpReplace($sFileText, "([^\r\n]+)", "' & _IncreaseByCallBack(""\1"") & '") & "'" ConsoleWrite(Execute($sRegExp1) & @LF) Func _IncreaseByCallBack($sMatch) Return "ITEM: " & $sMatch EndFunc If you need only to add "ITEM: " and that's it: ConsoleWrite("ITEM: " & StringReplace($sFileText, @CRLF, @CRLF & "ITEM: ") & @CRLF)
GEOSoft Posted November 21, 2009 Posted November 21, 2009 This may be it. I'm not at the system with AutoIt installed so I'm winging it here. $sFile = "somefile.txt" $sTxt = FileRead($sFile) $sTxt = StringRegExpReplace($sTxt, "(?m:^)(.+\v+|$)", "ITEM: $1") If @Extended Then ClipPut($sTxt) ;; If it's working right remove this and uncomment the next 3 lines ;$hFile = FileOpen($sFile, 2) ;FileWrite($hFile, $sTxt) ;FileClose($hFile) EndIf George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
Xenobiologist Posted November 21, 2009 Posted November 21, 2009 Local $sFileText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." & @CRLF & _ "Curabitur pulvinar malesuada metus, in sollicitudin libero pulvinar et." & @CRLF & _ "Nullam nisi lectus, ultrices ut fringilla in, consequat a dolor." & @CRLF & _ "Morbi pulvinar commodo fermentum. Nunc libero quam, elementum nec elementum eget, bibendum sed orci." & @CRLF & _ "Aliquam iaculis augue sed leo vehicula tincidunt." & @CRLF & _ "Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;" & @CRLF & _ "Fusce ut eros quam. Phasellus tempor odio lobortis eros ultrices varius." & @CRLF & _ "Nullam aliquet malesuada nibh, id molestie purus sagittis interdum." & @CRLF & _ "Mauris quam nisl, ultrices sit amet scelerisque auctor, scelerisque eget dolor." ConsoleWrite("ITEM: " & StringReplace($sFileText, @CRLF, @CRLF & "ITEM: ") & @CRLF) Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times
Dolemite50 Posted November 21, 2009 Author Posted November 21, 2009 Thanks alot guys, all solutions should work fine. My question was slightly hypothetical because I was also trying to clear up some newline/RegExp confusion.Geo's code is enlightening but I still have a few questions if anyone has a sec. (?m:^)(?m) - ^ and $ match newlines within data. Is (?m) used as a trigger to enable ^ and $ matching on lines? Would $ not match an EOL without it? I'm lost on the ":^" -part. The closest I could find in my ref books was (?:...) = Passive Group. I'm thinking that it ties in with the $1 backref somehow, is that right? Does $1 mean "Use the first (group), but ignore it if it is passive?"(.+\v+|$)My best guess is "Match any char one or more times until one or more vertical spaces or end of the line" Head... hurting... must.. abort...
GEOSoft Posted November 22, 2009 Posted November 22, 2009 (edited) Thanks alot guys, all solutions should work fine. My question was slightly hypothetical because I was also trying to clear up some newline/RegExp confusion.Geo's code is enlightening but I still have a few questions if anyone has a sec. (?m:^)(?m) - ^ and $ match newlines within data. Is (?m) used as a trigger to enable ^ and $ matching on lines? Would $ not match an EOL without it? I'm lost on the ":^" -part. The closest I could find in my ref books was (?:...) = Passive Group. I'm thinking that it ties in with the $1 backref somehow, is that right? Does $1 mean "Use the first (group), but ignore it if it is passive?"(.+\v+|$)My best guess is "Match any char one or more times until one or more vertical spaces or end of the line" Head... hurting... must.. abort...(?m:^) matches from the beginning of the line.EDIT:(.+\v+|$)My best guess is "Match any char one or more times until one or more vertical spaces or end of the line"Would have been coorect if you had said "end of the string." Edited November 22, 2009 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
Mison Posted November 23, 2009 Posted November 23, 2009 It's also good to explicitly specify the newline convention by starting a pattern string with (*CRLF) or (*ANYCRLF) because in PCRE, the default newline sequence is @LF(Unix). On Windows, the default newline sequence is @CRLF Hi ;)
GEOSoft Posted November 23, 2009 Posted November 23, 2009 It's also good to explicitly specify the newline convention by starting a pattern string with (*CRLF) or (*ANYCRLF) because in PCRE, the default newline sequence is @LF(Unix). On Windows, the default newline sequence is @CRLFStarting with a new line will usually miss the first line. If you meant ending with a new line (\n) then \v covers that. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
Mison Posted November 23, 2009 Posted November 23, 2009 Vertical whitespace character? Owh, I never knew that it can be used that way. Hi ;)
GEOSoft Posted November 23, 2009 Posted November 23, 2009 Vertical whitespace character? Owh, I never knew that it can be used that way.It covers 0x0a through 0x0d George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!"
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