This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Best way to match using regexp?

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Best way to match using regexp?

Post by awyeah »

I came up with this recently:

Code: Select all

if {[regexp -nocase {012-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]|013-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]|014-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]|016-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]|017-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]|019-?[0-9]{7}[^A-Z0-9+&@#/%?=~_|!:,.;]} $text]} {
The code looks blotty right. Okay the main idea behind this is to match cell phone numbers.

The cell operator numbers are: 012, 013, 014... and so on as you can see.
Then there is a 7 digit phone number after the cell operator number.
After the last digit (7th digit) of the phone number there shouldn't be any number.

The number can be like:
0126708090

or even like:
012-6708090

This piece of code looks very long and blotty to me, although it works. Any suggestions for improvement?
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Code: Select all

regexp {^01[234679]-?[0-9]{7}$} $number
So the number must start with a number (0) and end with a number (exactly 7 numbers after the operator).
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Thanks Sir_Fz that is exactly what I needed! Actually the "$" in the end did the trick, hehe :P
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
De Kus
Revered One
Posts: 1361
Joined: Sun Dec 15, 2002 11:41 am
Location: Germany

Post by De Kus »

thats strange, my cell phone number consists of 8 digits and has a 4 digit prefix number even without the country code. Is it supposed to match only local cellphone numbers?
De Kus
StarZ|De_Kus, De_Kus or DeKus on IRC
Copyright © 2005-2009 by De Kus - published under The MIT License
Love hurts, love strengthens...
User avatar
Sir_Fz
Revered One
Posts: 3794
Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:

Post by Sir_Fz »

Phone numbers differ from one country to another. For example, here in Lebanon a phone number is made up of 6 numbers, and our area code is made up of 2 numbers so it's +961-xx-xxxxxx.
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Exactly, Sir_Fz is right. This only matches cellphone providers in Malaysia. They have a 3 digit operator code following a 7 digit individual number. The fixed line numbers are however different.

Here is what I use now:

Code: Select all

if {[regexp {\x20(01[234679]-?[0-9]{7})\x20} $text]} {
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
Post Reply