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.

regexp issue

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

regexp issue

Post by iamdeath »

Hi

Can you tell me how can I regexp a number which contains certain things before and after for ex:


I want to look for "3" but look for 3 only when 3 has a number before it and an alphabet after it and turn 3 into THREE.

like this

11.6 3 test --> then turn 3 into Three

Turn Number three into Alphabet Three

but

if it's number 3 number then return
or if it's alphabet 3 alphabet return

Thanks
iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Re: regexp issue

Post by awyeah »

iamdeath wrote:Hi
like this:

11.6 3 test --> then turn 3 into Three
To match this you can use this:

Code: Select all

#if $text is the string: "11.6 3 test"

if {[regexp {^(([0-9.]{3,})|([0-9]{1,}))(\x20)(3)(\x20)([a-z]{1,})$} $text]} {

#for substitution you can use regsub or string map
#or you can do it this way:

set text [split $text]
set text [lindex $text 0] Three [lindex $text 2]
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Thanks for your reply awyeah but I dont think that will slve my problem ok let me tell you again, currently I am using this:

Code: Select all

regsub -all { 3 } $text " Three Runs " text
That will make " 3 " into " Three Runs ", but I want some changes like:

If 3 has numbers before it and alphabet after it then make that " 3 " into " Three Runs "

For ex:

Code: Select all

over 18.1 3 fuller 
over 19.4 3 This is a test
I want that " 3 " to be turned into Three Runs. So it will look like:

Code: Select all

over 18.1 Three Runs fuller 
over 19.4 Three Runs This is a test
Thanks
|AmDeAtH @ Undernet
Death is only the *Beginning*...
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Untested, but I guess this should do the trick, based on your examples:

Code: Select all

regsub -all {([[:digit:].]+) 3 ([[:alpha:]]+)} $text {\1 Three Runs \2} out
NML_375
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Well my idea was first to check with regexp to match the string and then proceed to substitute, you can substitute in many ways: regsub IS NOT the ONLY SOLUTION.

You can use nml's expression for regsub or use this then, lol:

Code: Select all

set text [lindex [split $text] 0] Three Runs [lrange [split $text] 2 end]
This should also do the work for you, matches the two output strings you showed.
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
User avatar
iamdeath
Master
Posts: 323
Joined: Fri Feb 11, 2005 2:32 pm
Location: *HeLL*
Contact:

Post by iamdeath »

Thanks alot nml, that solved the issue. awyeah I could use your expression but I don't know how to place it I was afraid I might disturb the whole code when regsub is easy and safe to use or remove :P

Thanks once again awyeah and nml.

iamdeath
|AmDeAtH @ Undernet
Death is only the *Beginning*...
Post Reply