Sign in to follow this  
Followers 0
Omatsei

Count?

8 posts in this topic

Is it possible to search through a string and find as many instances of a certain character? I'd like to parse a line of e-mail addresses, but sometimes there's only 1... so if I could search for the @ character, then put all the characters before it (going backwards until it reaches a space, comma, etc.), then all the characters after it (again, until it reaches a space or comma, or the end of the line) into a string...

The lines might look like this:

"John Smith" jsmithy@smith.com

"Smith Company" jsmithy@smith.com,jane.smith@smith.com

In the first time through, it'd take "John Smith" and put that into a variable as $name, then put jsmithy@smith.com into $emailaddress[1]. The second time, it'd put "Smith Company" into $name, then jsmithy@smith.com into $emailaddress[1], but then jane.smith@smith.com into $emailaddress[2]. Is that possible / easily done? It would be really easy, except I can't really search on any characters other than the @.... sometimes the names have commas and such in them, which screws with any StringSplit I've tried thus far.

I really hope that makes sense....

Share this post


Link to post
Share on other sites



Is it possible to search through a string and find as many instances of a certain character? I'd like to parse a line of e-mail addresses, but sometimes there's only 1... so if I could search for the @ character, then put all the characters before it (going backwards until it reaches a space, comma, etc.), then all the characters after it (again, until it reaches a space or comma, or the end of the line) into a string...

The lines might look like this:

"John Smith" jsmithy@smith.com

"Smith Company" jsmithy@smith.com,jane.smith@smith.com

In the first time through, it'd take "John Smith" and put that into a variable as $name, then put jsmithy@smith.com into $emailaddress[1]. The second time, it'd put "Smith Company" into $name, then jsmithy@smith.com into $emailaddress[1], but then jane.smith@smith.com into $emailaddress[2]. Is that possible / easily done? It would be really easy, except I can't really search on any characters other than the @.... sometimes the names have commas and such in them, which screws with any StringSplit I've tried thus far.

I really hope that makes sense....

(using your example)

You could try something along the lines of

$In = '"Smith Company" jsmithy@smith.com,jane.smith@smith.com'
$Name = StringLeft($In,StringInStr($In,Chr(34),'',-1)
$In = StringReplace($In,$Name,'')
$emailaddress[1] = StringLeft($In,StringInStr($In,",")-1)
$emailaddress[2] = StringReplace($In,$emailaddress[1] & ',','')

Of course you will have to refine the proceedure if there are more than 2 email addresses in $In

This is just an idea off the top of my head and I know it can be made easier yet.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Not tested but if you have a huge file with several email addresse in it this shoiuld return all that are valid. It's from the top of my head so it need some validation. (Or you could use google to find a better pattern..;) )

$arr = StringRegExp($data, "([\w\d._]+@[\w\d._]+)", 3)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

hobbyist style...

#include <GuiConstants.au3>

; as if read from a file
Dim $read[3]
$read[1] = '"John Smith" jsmithy@smith.com'
$read[2] = '"Smith Company" jsmithy@smith.com,jane.smith@smith.com'

; array/variable used
Dim $name[UBound($read) ][3]

For $x = 1 To UBound($read) - 1
    $split = StringSplit($read[$x], ' ')
    $name[$x][0] = $split[1] & " " & $split[2]
    If StringInStr($split[3], ",") Then
        $divide = StringSplit($split[3], ",")
        $name[$x][1] = $divide[1]
        $name[$x][2] = $divide[2]
    Else
        $name[$x][1] = $split[3]
    EndIf
Next


; Use the info
For $x = 1 To UBound($read) - 1
    MsgBox(64, "Name = " & $name[$x][0], " Email Address #1 = " & $name[$x][1] & @CRLF & " Email Address #2 = " & $name[$x][2], 5)
Next

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
Share on other sites

All 3 methods seem to work, but what if there's dozens, and sometimes hundreds of e-mail addresses in the line? After thinking about the problem last night, I came to the conclusion that I'll probably have to create a recursive parser.

alias John "Johnny Boy" jsmith@smith.com

alias "Smith Company" jsmith@smith.com (John Smith), jane.smith@smith.com (Jane Smith), joe.smith@smith.com

That's an example of the file I'm trying to parse... so I'm thinking it'd be easiest to start right at the beginning, then move slowly through the line, left to right, and analyze each character until I get all the information I need. That's a little over my head at the moment... For instance, I only take lines that start with the word "alias", then move past the space and analyze the next character, and depending on what it is, make my next move. If it's a double-quote, take everything within the double-quotes and assign it to the $name variable. If it's not a double-quote, take that word (until the next space) and assign it to $name. Then repeat the process for the next word / phrase and assign it to $nickname. Then do it again for $emailaddress... etc.

I'm thinking I need to start with the line, then do a for...next loop with StringLeft that increments by 1 each pass... Does that sound feasible?

Share this post


Link to post
Share on other sites

Let me ask a simpler question... I have a line of text that I want to parse. How can I step through the characters, one-by-one, and make decisions based on what each character is?

Share this post


Link to post
Share on other sites

Let me ask a simpler question... I have a line of text that I want to parse. How can I step through the characters, one-by-one, and make decisions based on what each character is?

Something like ..(untested)

for $x = 1 to StringLen($line)
     $Char = StringMid($Line,$x,1)
    ; do here the tests you want
Next

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

stringsplit($line, "")

8)


NEWHeader1.png

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