Jump to content
Nigges

Making a string in different variations to an arry

Recommended Posts

Nigges

Hello helpers and masterminds,

I`m a starter with autoit and found the help function of autoit so far very helpful. But I´m standing in front of a problem because I need help with programming and so far I didn´t find that problem on any other forum. 

My problem is that in autoit there are ways to split a string and make it to an array, but is there a ways to have string like a phone number like this "001 202 555 0120" and put this through a RegEx  into an array which looks like: [1]"0 012 025 550 120" , [2]"00 120 255 501 20", ... and so on.

The string should just be splited up and don´t be manipulated in another way.

My RegEx looks like this to filter german, swiss and french phone numbers:

'(\W?((?:^00|^\+)(?:33|41|49))?\W?\W?\W?\W?([0-9]{1})\W?\W?((?:[0-9]{1,3})\W)?([0-9]{2,5})\W([0-9]{1,10})\W?([0-9]{0,3})?)'

I hope someone will understand what I´m asking for and can help me.

 

Regards 

Nigges

Share this post


Link to post
Share on other sites
gruntydatsun

this works on your examples:
 

'\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d*\s*'

 

Share this post


Link to post
Share on other sites
Nigges

First thank you!

But how can I set this into code. 

I tried this :

$sString = "0041 (0)33 377 55 22"

$aNummern = StringRegExpReplace( $sString, '/(\W?((?:^00|^\+)(?:33|41|49))?\W?\W?\W?\W?([0-9]{1})\W?\W?((?:[0-9]{1,3})\W)?([0-9]{2,5})\W([0-9]{1,10})\W?([0-9]{0,3})?)/g', '\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d\s*\d*\s*')

For $i = 0 to 100
    ConsoleWrite(@CRLF&@CRLF&$aNummern[$i]&@CRLF&@CRLF)
Next

but the out put of :

Quote

"C:\Users\nsr\Programme\AutoIT\Angefangen\RegExTest.au3" (6) : ==> Subscript used on non-accessible variable.:
ConsoleWrite(@CRLF&@CRLF&$aNummern[$i]&@CRLF&@CRLF)
ConsoleWrite(@CRLF&@CRLF&$aNummern^ ERROR

doesn´t really help me. 

Why is my variable non-accessibe?

 

Regards

Nigges

 

Share this post


Link to post
Share on other sites
gruntydatsun

You're getting that error message because StringRegExpReplace returns a string, not an array.

what is an example of what you are trying to do with these numbers? 

by that I mean something like..... I want to take 0422003000 and format it to (0422) 003-000

below is an example of how to use it:

 

#include <array.au3>

$string = "0 012 025 550 120  with assorted 332 234 stuff and 00 120 255 501 20 and more 32234 and $sString = 0041 (0)33 377 55 22 and s22"
$pattern3 = '\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d\s*\(*\)*\d*\s*\(*\)*'

$array = StringRegExp($string,$pattern3,3)

if @error <> 0 then
    msgbox(16,"ERROR in ArrayDisplay",@error)
Else
    for $x = 0 to Ubound($array)-1
        ConsoleWrite("Array[" & $x & "] = " & $array[$x] & @CRLF)
    Next
EndIf

_ArrayDisplay($array,"$Array")

Share this post


Link to post
Share on other sites
Nigges
1 hour ago, gruntydatsun said:

You're getting that error message because StringRegExpReplace returns a string, not an array.

what is an example of what you are trying to do with these numbers? 

by that I mean something like..... I want to take 0422003000 and format it to (0422) 003-000

below is an example of how to use it:

Oh okay I´didn´t know that StringRegExpReplace just returns strings.

My example is something like I´m getting a number like 

$sString = "0041 (0)33 377 55 22"

and now I want to check any version of this number.

So I tried to create an RegEx which would take the upper number and gives this array out:

$array[1]  = "0041 0 33377 55 22"
$array[2]  = "0041 03 3377 55 22"
$array[3]  = "0041 033 377 55 22"
$array[4]  = "0041 0333 77 55 22"
$array[5]  = "0041 03337 7 55 22"
$array[6]  = "0041 033377 55 22"
$array[7]  = "0041 0 3337 755 22"
$array[8]  = "0041 03 337 755 22"
$array[9]  = "0041 033 37 755 22"
$array[10] = "0041 0333 7 755 22"
$array[11] = "0041 03337 755 22"
$array[12] = "0041 0 333 7755 22"
$array[13] = "0041 03 33 7755 22"
$array[14] = "0041 033 3 7755 22"

and so on.

I know that can be a very long array but I need to search trough a database with a lot of different phone numbers and the search isn´t working at this point.

At least it isn´t working as I want to because my program doesn´t find every number when they are saved in another way.

So I need this RegEx to get the likiest variant of the number I search with.

Share this post


Link to post
Share on other sites
gruntydatsun

ok, give this a try
 

dim $array[14] = ["0041 0 y33377 55 225","0041 03 33-77 55 22","(0041)033 377 55 22","(0041)-0333-77-55 22","0041 03337 7 55 22","0041 033377 55 22","0041 0 3337 755 22","0041 03 337 755 22","0041 033 37 755 22","0041 0333 7 755 22","0041 03337 755 22","0041 0 333 7755 22","0041 03 33 7755 22","0041 033 3 7755 22"]
$pattern3 = '\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*\d\s*\(*\)*\-*'

for $x = 0 to Ubound($array)-1
    if StringRegExp($array[$x],$pattern3,0) = 1 Then
        msgbox(1,"OK","Pattern matches number: " & $array[$x])
    Else
        msgbox(16,"FAIL","Pattern fails to match number: " & $array[$x])
    EndIf
Next

 

Share this post


Link to post
Share on other sites
jchd

You ought to store the phone numbers without any space in the DB and search similarly, removing whitespaces in the searched value. Only then you can format the found number according to the locale convention if you have to display it on some UI.

Which DB engine do you use?

 

Edited by jchd

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
gruntydatsun

The pattern is just this

\d\s*\(*\)*\-*

pasted in 14 times, however many numbers there will be in the phone number

if you have the potential for say 14 - 16 numbers in a phone number just paste it in 16 times and on the last two put a * after the d to make those two digits optional

Share this post


Link to post
Share on other sites
Nigges

Oh wow!

Thank you very much gruntydatsun! That is very helpful to understand how to sort out but 

I´m trying to make an array out of a variation of just one string. You made an array and checked it if it is like your pattern.

Can´t I just take my string and change it into an array which has every version of this pattern in it? And if yes how?

9 minutes ago, jchd said:

You ought to store the phone numbers without any space in the DB and search similarly, removing whitespaces in the searched value. Only then you can format the found number according to the locale convention if you have to display it on some UI.

Which DB engine do you use?

The DB is really big and I don´t want to change the hole DB only for my search function( and I can´t because Im not allowed to).

I know how to create a string within only numbers and without whitespaces, etc. but in the DB I would have to create a hole new column where every phone number is saved without any whitespaces, etc. 

I think I´m not allowed to do that. :D

Share this post


Link to post
Share on other sites
SlackerAl

I don't want to derail this reg exp fest, but could you not just run a query along the lines of

SELECT * FROM telnumbers WHERE REPLACE(number, ' ', '') = REPLACE($test_number, ' ', '')

that would compare your number without spaces for an equivalent number regardless of spaces in the db.

Edit: Obviously not tested, but have a look at SQL REPLACE and figure out exact syntax

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites
Nigges

Oh okay SlackerAl that is actually also good I will try this tomorrow and reply if it worked.

Thank all of you very much!

Share this post


Link to post
Share on other sites
SlackerAl

OK, well just so you are aware, that will of course load up the database quite nicely if that is a very large table. @jchd is correct, the best way would be to add a new field once without the spaces and query that. If a single execution of your code is going to make a large number of queries you could consider creating a temporary table to save the WHERE overhead.

Edited by SlackerAl

Problem solving step 1: Write a simple, self-contained, running, replicator of your problem.

Share this post


Link to post
Share on other sites
Nigges

Okay thank you.

But I´m done for today so tomorrow we will see how and which way is going to lead me.

Thank you and maybe hear from each other tomorrow. :D

Share this post


Link to post
Share on other sites
gruntydatsun

Stripping both the value from your database and your input to digits sounds good.  if you can only query the database, below is one way to do it on this end without needing an array of every permutation:
 

dim $array[14] = ["0041 0 y33377 55 225","0 04   1 03      33-77 5 5 22","(0041)033 377 55 22","(0041)-0333-7 7-5 5 22","00( 41 )-03337 7 55 22","0041 033377 55 22","0041 0 3337 755 22","0041 03 337 755 22","0041 033 37 755 22","0041 0333 7 755 22","0041 03337 755 22","0041 0 333 7755 22","0041 03 33 7755 22","0041 033 3 7755 22"]
$input = "0041 (0)33 377 55 22"                                         ;number you provide
$regex = "[\(\)\-\s]"                                                   ;regular expression

for $element in $array
    if StringRegExpReplace($element,$regex,"") = StringRegExpReplace($input,$regex,"") Then
        msgbox(1,"OK",$element & " matches " & $input)
    Else
        msgbox(16,"FAIL",$element & " does NOT match " & $input)
    EndIf
Next

 

Share this post


Link to post
Share on other sites
Nigges

Well that's very good gruntydatsun!

I think to get the values out of the database and strip it shouldn't be a problem because to that I have access.

Thank you very much for your help. That's very kind!^_^

A big "thank you!" to everyone who helped me.

Regards

Nigges

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

×