467,114 Members | 1,320 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,114 developers. It's quick & easy.

Help needed in RegExp

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";
  8. }
Output :

expected Output:

If it is na1_me also it should work.
Jun 17 '08 #1
  • viewed: 884
4 Replies
Expert Mod 2GB
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;
  6. }
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.



**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
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


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
Expert 256MB
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.  }
Jun 17 '08 #4
Expert 2GB
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. }
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.

Similar topics

5 posts views Thread by Iain Downie | last post: by
6 posts views Thread by paulsmith5@hotmail.com | last post: by
1 post views Thread by ric.castagna@gmail.com | last post: by
2 posts views Thread by Tamas Nyilanszky | last post: by
7 posts views Thread by VUNETdotUS | last post: by
3 posts views Thread by Happy Face | last post: by
10 posts views Thread by Stimp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.