Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Regular expressions - substring

  • 13-08-2007 11:51AM
    #1
    Registered Users, Registered Users 2 Posts: 1,127 ✭✭✭


    Guys

    Am googling for the last couple of hours, no joy. Maybe someone can provide an epiphany.

    I have this list of numbers in the format XXXXXX,XXXXXXXXXXXX, and I need to use regex to extract everything after the comma, to the end of the line.

    Any thoughts?


Comments

  • Registered Users, Registered Users 2 Posts: 604 ✭✭✭Kai


    I dont think reg ex is what your looking for. Reg ex looks for patterns but you cant really use it the way you want to.
    What programming language are you using.
    in c# you could return the last digits like this
    private string ParseNumber(string numberToParse)
    {
    	int commaLocation = numberToparse.IndexOf(",");
    	string returnValue ="";
    	if(commaLocation > -1 & numberToParse.Length > commaLocation + 1)
    	{
    		returnValue = numberToParse.SubString(commaLocation +1);
    	}
    	return returnValue;
    }
    


  • Registered Users, Registered Users 2 Posts: 68,190 ✭✭✭✭seamus


    Most regexp functions provide a means by which you "tag" sections of the string which match the regexp, then you can extract those tagged bits.

    The regexp may look something like this:
    /^\d*,(\.*)$/
    So the function will attempt to match the entire expression, but only return the bit in parentheses.


  • Registered Users, Registered Users 2 Posts: 23,212 ✭✭✭✭Tom Dunne


    As an alternative, what string handling functions does the languages have?

    Look for INSTR, LEN and SUBSTR.

    INSTR is usually of the format INSTR(string, ',') and will return the location of the comma (in this case) within string. You now know where the comma is, so you look from this point to the end of the string for what you want.

    It would be something like:

    Newstring:= substr ( INSTR(string,','), LEN(string) )


  • Registered Users, Registered Users 2 Posts: 90 ✭✭CasimiR


    You could also split the string on the ","
    Depending on the language you re using : strtok , split, explode ...

    eg in perl
    $t = '123455,789012345678';
    $a = (split(/,/, $t))[1];
    


  • Registered Users, Registered Users 2 Posts: 6,605 ✭✭✭daymobrew


    seamus wrote:
    The regexp may look something like this:
    /^\d*,(\.*)$/
    So the function will attempt to match the entire expression, but only return the bit in parentheses.
    Here is an example of this in perl.
    #!/usr/bin/perl -w
    
    my $var = '123455,789012345678';
    print $var, "\n";
    # A substitution that discards what is before the comma.
    $var =~ s/^\d+,//;
    print $var, "\n";
    
    $var = '123455,789012345678';
    print $var, "\n";
    # A match check that captures the numbers after the comma.
    if ( $var =~ /^\d+,(\d+)$/ ) {
      $var = $1;
    }
    print $var, "\n";
    


  • Advertisement
Advertisement