Sign in to follow this  
Followers 0
DrewSS

String Trim or RegExp?

6 posts in this topic

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



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)
1 person likes this

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

Share this post


Link to post
Share on other sites

Dang, that did the trick. Thank you!!

Share this post


Link to post
Share on other sites

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.

1 person likes this

[center]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.[/center]

Share this post


Link to post
Share on other sites

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

1 person likes this

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

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 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  
Followers 0