Solved

tcl+ regexp

Posted on 2004-04-13
8
572 Views
Last Modified: 2013-11-22
Hello,
   I have a question regd regexp in TCL.  

 There is a sring   var = " 1 ABC 2 BCN 3 NNN ooo llll mmm "
  I have to grep [NUMBERS] [ CAPS LETTESR] .
so i'm using  regexp { [0-9]+ [A-z]+ } $var var1
but it greps only 1 ABC . I need to grep all (i.e) 2 BCN 3 NNN
any simple logic or any power usage in regexp.Pls reply

0
Comment
Question by:n_uthra25
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:leflon
ID: 10812388
Hi n_uthra25,

{[0-9]+[ 0-9A-Z]+}
will match any string starting with a number, followed by any combination of blanks, numbers or uppercase letters.
this should match "1 ABC 2 BCN 3 NNN " (ifear it will include the last blank)

leflon
0
 

Author Comment

by:n_uthra25
ID: 10812415
Hello Lefon,
   If you see in $var1 we will find only 1 ABC . But i need 2 BCN 3 NNN ( all the possibilities)  in some variable var2 var3.. so how do to .
  Hope you know regexp always grep first string.

0
 
LVL 7

Expert Comment

by:leflon
ID: 10812437
n_uthra25,

ahhh, got it (at last) :)
checking..... (will take some time)

leflon
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 28

Expert Comment

by:FishMonger
ID: 10813901
I don't know what the differences are between TCL's regex engine and Perl's but this is how I'd do it with Perl.

(@vars) = $var =~ /\G\s*(\d+\s+[A-z]+)/g;
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 10813929
This is the test code I used and its output.


#!/usr/bin/perl -w

use Data::Dumper;

$var = " 1 ABC 2 BCN 3 NNN";

(@vars) = $var =~ /\G\s*(\d+\s+[A-z]+)/g;
print Dumper @vars;

----- output -----
$VAR1 = '1 ABC';
$VAR2 = '2 BCN';
$VAR3 = '3 NNN';
0
 

Author Comment

by:n_uthra25
ID: 10814064
Hello FishMonger,
   I would really appreciate if could get it in TCL.

0
 
LVL 7

Accepted Solution

by:
leflon earned 80 total points
ID: 10814617
hi n_uthra25

if i get it right you can use the -inline and -all switches of the regexp to get a list with all matches

with set var " 1 ABC 2 BCN 3 NNN ooo llll mmm "
regexp -all -inline -- {[0-9]+ [A-z]+} $var
  => {1 AAA} {2 BCN} {3 NNN}

hth
leflon
0
 

Author Comment

by:n_uthra25
ID: 10815445
great..Thanks for your reply..
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question