Jump to content

copy starting with any letter instead of word


Recommended Posts

i have this script, it copies and saves all lines starting with the word "valuable"

how can i make it copy and save all lines starting with any character?

#include <File.au3>
#include <Array.au3>

$file_output = @DesktopDir & "\output.txt"
Local $mypath = @ScriptDir, $lines = ""
Local $aFiles = _FileListToArrayRec($mypath, "*.txt", 1, 1, 0, 2)

For $i = 1 to $aFiles[0]
   $content = FileRead($aFiles[$i])
   $res1 = StringRegExp($content, '(?m)^(valuable\N*)\R?', 3)

   If IsArray($res1) Then
         $lines &= $aFiles[$i] & @CRLF
         $lines &= _ArrayToString($res1, @CRLF) & @CRLF & @CRLF
   EndIf
Next

FileWrite($file_output, $lines)
Link to comment
Share on other sites

Try altering the regexp:

'(?m)^(' & $sMyLetter & '\N*)\R?'

I can't test it ATM - unfortunately.

A word of warning:  some characters have special significance in regexp patterns. The code should work for alphanumeric input. Regexp is also case sensitive. The following change makes the pattern case insensitive.

'(?i)(?m)^(' & $sMyLetter & '\N*)\R?'

Substitute the regexp into the code you have. Declare the variable $sMyLetter and assign it equal to the character you are looking for. Run the code and see what happens.

Edited by czardas
Link to comment
Share on other sites

'(?im)^([\d-]\N*)\R?'

This will work for lines starting with hyphen or any digit

For easier use you should build a little func with the desired pattern as parameter :)

$aLines = _LinesStartingWith($txt, '[\d-]')

Func _LinesStartingWith($content, $sMyPattern)
   $res = StringRegExp($content, '(?m)^(' & $sMyPattern & '\N*)\R?', 3)
   If not IsArray($res) Then Return SetError(0)
   Return $res
EndFunc
Edited by mikell
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...