DrewSS Posted November 18, 2014 Posted November 18, 2014 Hello, Sorry for the newbie question, but I'm running out of time and could really use the assistance. I'm trying to trim all characters after the first 8 characters on every line of a text file (some lines do not have the descriptions.) The list looks like this: 394-4885 random descriptions 390-7454 391-5007 random descriptions 394-3021 398-6879 random descriptions 390-3547 random descriptions 390-0450 392-6940 Here is my code, but its not working at all. What am I doing wrong? $sFilePath3 = @ScriptDir & "list.txt" FileOpen($file, 0) For $i = 1 to _FileCountLines($file) $string= FileReadLine($file, $i) StringTrimLeft($string, StringLen($string) - 8) Next FileClose($file)
Moderators Solution JLogan3o13 Posted November 18, 2014 Moderators Solution Posted November 18, 2014 If you want the numbers only (phone number, I'm assuming), and they're always in the same spot, try this: $sString = "367-1111 random blah blah blah" ConsoleWrite(StringLeft($sString, 8) & @CRLF) DrewSS 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
DrewSS Posted November 18, 2014 Author Posted November 18, 2014 Dang, that did the trick. Thank you!!
Moderators SmOke_N Posted November 18, 2014 Moderators Posted November 18, 2014 If the numbers are always at the beginning of the line, and they are always only 8 characters, you have the simplest solution above. Here's a regex method to return an array, it doesn't assume that the phone number will be always at the beginning of the line. It also makes exception for different types of setups. eg. area code + prefix + line number prefix + line number You can see below. #include <Array.au3> Global $gsz_Str = "394-4885 random descriptions" & @CRLF $gsz_Str &= "(555)390-7454" & @CRLF $gsz_Str &= "391-5007 random descriptions" & @CRLF $gsz_Str &= "555.394.3021" & @CRLF $gsz_Str &= "398-6879 random descriptions" & @CRLF $gsz_Str &= "555-390-3547 random descriptions" & @CRLF $gsz_Str &= "390-0450" & @CRLF $gsz_Str &= "(555) 392-6940" Global $ga_Numbers = StringRegExp($gsz_str, "(?m)^.*?((?:(?:\W)?\d{3}\W+)?\d{3}\W\d{4})", 3) _ArrayDisplay($ga_Numbers) It's been a while since I've been able to play with regex, forgot how much I enjoyed it. DrewSS 1 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Moderators JLogan3o13 Posted November 18, 2014 Moderators Posted November 18, 2014 It's been a while since I've been able to play with regex, forgot how much I enjoyed it. It is one of those things I keep telling myself I need to become more proficient at, but unfortunately it still makes my eyes bleed... MikahS 1 "Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball How to get your question answered on this forum!
DrewSS Posted November 18, 2014 Author Posted November 18, 2014 (edited) #include <Array.au3> Global $gsz_Str = "394-4885 random descriptions" & @CRLF $gsz_Str &= "(555)390-7454" & @CRLF $gsz_Str &= "391-5007 random descriptions" & @CRLF $gsz_Str &= "555.394.3021" & @CRLF $gsz_Str &= "398-6879 random descriptions" & @CRLF $gsz_Str &= "555-390-3547 random descriptions" & @CRLF $gsz_Str &= "390-0450" & @CRLF $gsz_Str &= "(555) 392-6940" Global $ga_Numbers = StringRegExp($gsz_str, "(?m)^.*?((?:(?:\W)?\d{3}\W+)?\d{3}\W\d{4})", 3) _ArrayDisplay($ga_Numbers) Wow this I shall save, thank you. The numbers arent actually phone numbers, but this helps clarify regex a lot. edit: a lot Used this method and works excellently! Edited November 19, 2014 by DrewSS
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