Jump to content
Sign in to follow this  
DrewSS

String Trim or RegExp?

Recommended Posts

DrewSS

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)

Share this post


Link to post
Share on other sites
JLogan3o13

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)
  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
SmOke_N

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.

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

Share this post


Link to post
Share on other sites
JLogan3o13

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

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
DrewSS

 

#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 by DrewSS

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.