LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-20-2009, 09:19 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 366

Rep: Reputation: 32
TCL lsearch -regexp


Am trying to test whether any one of the items in a list contains the word "hello" but failed. Any idea why?

Here is the actual code:
if { [lsearch -regexp $line *hello*] >= 0 } {
}

This is what I got:
Invocation of "<procname>" failed with error "couldn't compile regular expression pattern: ?+* follows nothing"

In fact I want to do the following. Basically there are a number of digits preceding "hello", followed by one digit.
if { [lsearch -regexp $line [0-9]+hello[0-9]] >= 0 } {
}
 
Old 11-20-2009, 12:09 PM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 59
1/ Are you sure $line is a list?
2/ Which version of Tcl are you using 8.4? 8.5?
3/ *hello* is not a valid regexp. Check out http://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm: * has special meaning.
If you mean "any number (incl none at all) of random characters", then use .*
If you mean "any non-zero number of random chars", then use .+
4/ If you intend to use the 2nd pattern with the digits, why try the first one anyhow?

The error clearly marks your regular expression as the culprit.
 
Old 11-21-2009, 10:30 AM   #3
grob115
Member
 
Registered: Oct 2005
Posts: 366

Original Poster
Rep: Reputation: 32
Ah I see. Actually this is the original form I tried but still doesn't work. I want to match a pattern such as 1234hello1 or 2345hello2, etc. And yes $line is a list.

Any idea why [0-9]+hello[0-9] wouldn't work?

if { [lsearch -regexp $line [0-9]+hello[0-9]] >= 0 } {
}

Quote:
Originally Posted by timmeke View Post
1/ Are you sure $line is a list?
2/ Which version of Tcl are you using 8.4? 8.5?
3/ *hello* is not a valid regexp. Check out http://www.tcl.tk/man/tcl8.4/TclCmd/re_syntax.htm: * has special meaning.
If you mean "any number (incl none at all) of random characters", then use .*
If you mean "any non-zero number of random chars", then use .+
4/ If you intend to use the 2nd pattern with the digits, why try the first one anyhow?

The error clearly marks your regular expression as the culprit.
 
Old 11-22-2009, 08:31 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,340

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by grob115 View Post
Any idea why [0-9]+hello[0-9] wouldn't work?
if { [lsearch -regexp $line \[0-9\]+hello\[0-9\]] >= 0 } {
}

For me it works now

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RegExp help HarryBoy Linux - Newbie 1 02-16-2009 11:31 AM
regexp help cliff76 Linux - Newbie 3 03-07-2008 03:15 PM
Help with regexp anupamsr Linux - Software 4 03-05-2008 07:43 AM
regexp ovince Programming 8 06-17-2007 11:54 AM
Cannot find Tcl dll ( Perl's Tcl bridge ) Xyem Linux - Software 2 08-08-2006 10:45 AM


All times are GMT -5. The time now is 01:16 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration