Jump to content

StringRegExp & Phone Numbers


Recommended Posts

I'm using the following code to see if there is a phone number (in one of the following formats - it is not supposed to grab the 1 if the number starts with it) on the clipboard:

9515554356

(909)555-8790

(949) 555-5642

714-555-1324

(323) 555 0946

1 (626) 555 8790

1 864-555-7689

15825556330

$aPhoneOnClip = StringRegExp(ClipGet(), "[2-9]\d{9}|\d{3}-\d{3}-\d{4}|\(\d{3}\) {0,1}\d{3}[-| ]{0,1}\d{4}\b", 1)
;ConsoleWrite("@error for $aPhoneOnClip StringRegExp = " & @error & @CRLF)
If @error = 0 Then 
    $phoneNumber = $aPhoneOnClip[0]
    ConsoleWrite("$phoneNumber = " & $phoneNumber & @CRLF)
    $phoneNumber = StringRegExpReplace($phoneNumber, "\(|\)| |-", "")
    ConsoleWrite("$phoneNumber = " & $phoneNumber & @CRLF)
    $phoneNumber = InputBox("Dial Phone Number", "Enter Phone Number:", $phoneNumber)
Else
    $phoneNumber = InputBox("Dial Phone Number", "Enter Phone Number:", "XXX-XXX-XXXX")
EndIf

I have software on my computer that dials my phone for me so all I need is a number in 10-digit format, no symbols or space or anything - the software is picky. The idea is that if someone sends a message thru IM or email I can just copy that section of the text and press my windows hotkey which while execute the script, grab the phone number from the clipboard - if it's there, then either automatically dial it or ask me if I want to dial it. If there is no number on clipboard (@error <> 0) then just prompt for the number to dial. The script runs fine if there's a number on the clipboard that matches one of the formats and does exactly what it is supposed to do if there are no matches, but that's only if I comment out that second line, the ConsoleWrite.

Questions:

1) Does anyone see a better way to write the StringRegExp in regards to the specific phone number formats I'm looking for? I suppose it doesn't really make a difference, but this is my first time using StringRegExp so any advice is appreciated ^_^ The phone number could be given in a single line or somewhere inside a paragraph.

2) (I stumbled upon this problem while trying to tweak the StringRegExp) If I uncomment that second line and there is no match on the clipboard, the ConsoleWrite tells me @error = 1, which is expected. But, the rest of the code that checks for @error = 0 still runs. Does ConsoleWrite Set error back to 0 for some reason? I replaced ConsoleWrite with a MsgBox and got the same result. That's not really my main question, but I'm still curious... I'm sure it's something stupid I just missed in the help file and I couldn't find it in the forums either.

Thanks!

Link to comment
Share on other sites

Does anyone see a better way to write the StringRegExp

If it's working as it should, then this is the better way ;)

Does ConsoleWrite Set error back to 0 for some reason?

Yes, because there is no error in writing to the console ^_^ - Basically, all functions that does not set error (base on help file's remarks), will set the @error to 0 anyway, because there is no error.

So you can hold the @error in variable to workaround this:

$aPhoneOnClip = StringRegExp(ClipGet(), "[2-9]\d{9}|\d{3}-\d{3}-\d{4}|\(\d{3}\) {0,1}\d{3}[-| ]{0,1}\d{4}\b", 1)
$iError = @error

ConsoleWrite("@error for $aPhoneOnClip StringRegExp = " & @error & @CRLF)

If $iError = 0 Then
    $phoneNumber = $aPhoneOnClip[0]
    ConsoleWrite("$phoneNumber = " & $phoneNumber & @CRLF)
    $phoneNumber = StringRegExpReplace($phoneNumber, "[\(\) -]", "")
    ConsoleWrite("$phoneNumber = " & $phoneNumber & @CRLF)
    $phoneNumber = InputBox("Dial Phone Number", "Enter Phone Number:", $phoneNumber)
Else
    $phoneNumber = InputBox("Dial Phone Number", "Enter Phone Number:", "XXX-XXX-XXXX")
EndIf

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • Moderators

#include <array.au3>

Global $s_text = ClipGet()

; Remove non digit chars except cr/lf
Global $s_pat_replace = "[\h\(\)-]"
$s_text = StringRegExpReplace($s_text, $s_pat_replace, "")

; Get my list of numbers
Global $s_pat_regex = "1?(\d{10})(?:\z|\v+)"
Global $a_sre = StringRegExp($s_text, $s_pat_regex, 3)

_ArrayDisplay($a_sre)

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

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