Sign in to follow this  
Followers 0
Madness

StringRegExpReplace - start of file?

6 posts in this topic

I need to search for a heading in a text file and delete everything from the beginning of the file to the heading. Is there coding in regular expressions to do this? I see some REs support ^^ as the beginning of a file. Or should I be doing this a different way. I'm reading the contents of a file using FileRead into a variable, then working on the variable, and writing this back to a file. I need to run numerous globals on this, plus remove some items.

Also, are there limits to the amount of text using FileRead that can be read into a string variable?

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Looks like this does the trick:

(?s)(?m)^.*search item

Still would like to know if there are any limits in the file size that can be read with FileRead though.

Edited by Madness

Share this post


Link to post
Share on other sites

$sStr = stringRegExpReplace(FileRead("somefile.txt"), "(?i)(?s)^.*?(search string.*)$", $1)


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!"

Share this post


Link to post
Share on other sites

I wrote a function that behaves like string split except the deliminator is a regular expression.

You will just need to query the resulting array for what you want.

#include <Array.au3>

;~ ---for example
$sString="abc124bcd2cde3def4efg"

$sRegExp="[0-9]"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)

$sRegExp="124"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)

$sRegExp="[cd]"
$aResult=_REsplit($sString,$sRegExp)
_arraydisplay($aresult)
;~------

Func _RESplit($sString, $sRegExp, $iRegExBehave=3 )
    Local $aReturn[1]
    $aReturn[0]=$sRegExp

    $aSplits = StringRegExp($sString, $sRegExp,$iRegExBehave)

    For $x = 0 To UBound($aSplits) - 1
        if StringInStr($sString, $aSplits[$x]) then
            $iLeft = StringInStr($sString, $aSplits[$x])-1
            $iRight = StringLen($sString)-($iLeft+stringlen($aSplits[$x]))


            _ArrayAdd($aReturn, StringLeft($sString, $iLeft))
            $sString = StringRight($sString, $iRight)


        EndIf

    Next
    _ArrayAdd($aReturn, $sString)
    Return($aReturn)
EndFunc   ;==>_RESplit

Share this post


Link to post
Share on other sites

$sStr = stringRegExpReplace(FileRead("somefile.txt"), "(?i)(?s)^.*?(search string.*)$", $1)

Thanks George. I really like your Toolkit. Nice job!

Share this post


Link to post
Share on other sites

You're welcome and thanks for the comment about the toolkit.


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!"

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  
Followers 0