Jump to content

String Trim or RegExp?


DrewSS
 Share

Go to solution Solved by JLogan3o13,

Recommended Posts

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)

Link to comment
Share on other sites

  • Moderators
  • Solution

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)

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

Link to comment
Share on other sites

  • Moderators

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.

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.

Link to comment
Share on other sites

  • Moderators

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

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

Link to comment
Share on other sites

 

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