Help needed in RegExp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kriz4321
    New Member
    • Jan 2007
    • 48

    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,Alp habets or Numerals. Not sure why $2 prints "n" in the below code.

    [code=perl]
    $var="test.tc__ na1me_1.2008061 6184340025";
    if($var=~/(tc)__([a-z,A-Z,0-9])/)
    {
    print $2;
    print $3;----> #Why no output for $3
    print "came";

    }
    [/code]
    Output :
    ncame

    expected Output:
    na1me

    If it is na1_me also it should work.
    Last edited by numberwhun; Jun 17 '08, 11:53 AM. Reason: Please use code tags!
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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:

    [code=perl]
    $var="test.tc__ na1me_1.2008061 6184340025";
    if($var=~/^\d+_+(a-zA-Z0-9)+_+.+$/)
    {
    print $1;

    }
    [/code]

    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.
    Last edited by numberwhun; Jun 17 '08, 01:14 PM. Reason: update

    Comment

    • kriz4321
      New Member
      • Jan 2007
      • 48

      #3
      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__n a1me_1.20080616 184340025";

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

      Currently Iam not able to do this. Any help will be useful.

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        The following code will do the job.

        [CODE=text]
        $var="test.tc__ na1me_1.2008061 6184340025";
        if($var=~/\.tc__([a-zA-Z0-9]+)_/)
        {
        print $1;
        }
        [/CODE]
        Last edited by nithinpes; Jun 17 '08, 02:08 PM. Reason: tags

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          if there can be any non space characters between tc_ and _(digit):

          [code=perl]
          if ($var =~ /\.tc_+(\S+)_+\d/) {
          print $1;
          }
          [/code]

          if you know the exact amount of underscores there should be in the pattern remove '_+' and replace them with the number of underscores.

          Comment

          Working...