By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

Help needed in RegExp

P: 48
I need to get the content present between .tc_XXXX_1(Rexp should be tc followed by the content followed by _and a digit) in the $var

XXXX can contain specialchar,Alphabets or Numerals. Not sure why $2 prints "n" in the below code.

Expand|Select|Wrap|Line Numbers
  1. $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/(tc)__([a-z,A-Z,0-9])/)
  3. {
  4. print $2;
  5. print $3;----> #Why no output for $3
  6. print "came";
  7.  
  8. }
  9.  
Output :
ncame

expected Output:
na1me

If it is na1_me also it should work.
Jun 17 '08 #1
Share this Question
Share on Google+
4 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Well, it looks like your regular expression really needs some help.

To begin with, you asked why $3 has nothing. That is because there is no $3. From the match you specified, you only have two sets of (). The first one is $1 and the second one is $2. You did not specify anything to be saved as a $3. You could re-write the match to be the following, but please keep in mind that this is untested and might need some tweaking:

Expand|Select|Wrap|Line Numbers
  1.  $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/^\d+_+(a-zA-Z0-9)+_+.+$/)
  3. {
  4. print $1;
  5.  
  6. }
  7.  
If the above code works, it will print out the na1me that you expected. Albeit, the code is a little greedy, but it will hopefully get the job done. If I have time to test it, I will do so and correct any errors. But, in the middle match (a-zA-Z0-9), you mentioned special characters, you would have to specify exactly which special characters to look for, or you could possibly do it like this: (\w\W)*
but again, that's untested and I don't have a sample containing special characters from you either.

Regards,

Jeff

**Update: Ok, that regex doesn't work. I notice now some issues with it, but don't have the time right now to look at it. If someone else has the answer for the OP, please post it.
Jun 17 '08 #2

P: 48
Thanks. I thought $2 will print na1me but it prints only "n". I shouldnot have asked about $3. Also the code given by you is not working. I will check it out and see why it fails.

About special charac it can be any so we need to match with the following conditions to get "na1me" form the $var below

var="test.tc__na1me_1.20080616184340025";

charaqcters follwed after .tc_
characters before _1(_any digit) should give me exactly what I need Irrespective of it has numerals,Aplhabets or special characters in it.

Currently Iam not able to do this. Any help will be useful.
Jun 17 '08 #3

nithinpes
Expert 100+
P: 410
The following code will do the job.

Expand|Select|Wrap|Line Numbers
  1. $var="test.tc__na1me_1.20080616184340025";
  2. if($var=~/\.tc__([a-zA-Z0-9]+)_/)
  3. {
  4. print $1;
  5.  }
  6.  
Jun 17 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
if there can be any non space characters between tc_ and _(digit):

Expand|Select|Wrap|Line Numbers
  1. if ($var =~ /\.tc_+(\S+)_+\d/) {
  2.    print $1;
  3. }
  4.  
if you know the exact amount of underscores there should be in the pattern remove '_+' and replace them with the number of underscores.
Jun 17 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.