The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl RegExp to remove last character from strings ospreyeagle Shell Programming and Scripting 2 04-03-2008 02:55 PM
Matching character felixwhoals UNIX for Dummies Questions & Answers 2 01-09-2008 07:11 PM
GnuWin32 sed 4.1.4 regexp matching Simerian UNIX for Dummies Questions & Answers 2 06-23-2005 12:28 AM
special character ? mile1982 High Level Programming 1 10-19-2004 08:15 AM
character matching Rukshan UNIX for Dummies Questions & Answers 1 09-19-2000 09:03 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-09-2008
sumitgarg sumitgarg is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 18
tcl: regexp matching special character

Hello Experts,

Can someone help me here:

I have a variable which contains a string with "[]".

set var1 {a[1]}
set str1 {a[1] is the element i want to match}

Now "regexp $var1 $str1" does not work?
("regexp {a\[1\]} $str1" works, but var1 gets it's value automatically from another script)

Is there a way to make it work?
(adding backslash "\" to var1 might not be feasible)
  #2 (permalink)  
Old 01-09-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,518
Code:
% regexp {[$var1]} $str1
1
  #3 (permalink)  
Old 01-09-2008
sumitgarg sumitgarg is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 18
doesn't look right to me

Can you explain 'How it works?'?

It doesn't seem right to me.

% set var1 {a[1]}
a[1]
% set str1 {a[1] is the element i want to match}
a[1] is the element i want to match
% regexp $var1 $str1
0
% regexp {[$var1]} $str1
1
% set var1 {a[10]}
a[10]
% regexp {[$var1]} $str1
1
% set var1 {a[9080]}
a[9080]
% regexp {[$var1]} $str1
1
  #4 (permalink)  
Old 01-09-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
If you want a substring exact match that doesn't really rely on regexp syntax look at string match instead, but ymmv. I'm having trouble obtaining a sane result.You can always brute force the search..something like.

Code:
proc paramSearch {pattern str} {
set t [string length $pattern]
                for {set i 0} {$i < [string length $str]} {incr i $t} {
                    if {[string compare $pattern [string range $str $i [expr $i + [ expr $t - 1]]]] == 0} {return 1; #puts "Match at string index: $i"}
                    #puts "Compared $pattern and [string range $str $i [expr $i + [ expr $t - 1]]]"
                }
}
() 92 % set vv
thisis[]a[1]test
() 93 % set pat
a[1]
() 94 % paramSearch $pat $vv
1
  #5 (permalink)  
Old 01-09-2008
ramen_noodle ramen_noodle is offline Forum Advisor  
Registered User
  
 

Join Date: Dec 2007
Location: Virginia, USA.
Posts: 251
Off by one...
Code:
proc paramSearch {pattern str} {
set t [expr [string length $pattern] - 1]
                for {set i 0} {$i < [string length $str]} {incr i $t} {
                    if {[string compare $pattern [string range $str $i [expr $i + $t]]] == 0} {return 1; #puts "Match at string index: $i"}
                    #puts "Compared $pattern and [string range $str $i [expr $i + $t]]"
                }
}
  #6 (permalink)  
Old 01-09-2008
sumitgarg sumitgarg is offline
Registered User
  
 

Join Date: Feb 2005
Posts: 18
i would rather use lsearch

i was trying to understand if (and how) that can be done with 'regexp'?

Otherwise,

lsearch -exact $str1 $var1

does the job pretty well.

% set var1 {a[1]}
a[1]
% set var2 {aa[1]}
aa[1]
% set var3 {a[11]}
a[11]
% set str1 {a[1] is the element we are looking for and not aa[1] or a[11]}
a[1] is the element we are looking for and not aa[1] or a[11]

% lsearch -exact $str1 $var1
0
% lsearch -exact $str1 $var2
10
% lsearch -exact $str1 $var3
12
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0