Results 1 to 10 of 10

Thread: Help with string.find or string.sub.

  1. #1

    Default Help with string.find or string.sub.

    In my lua file I'm trying to convert strings by stripping off everything to the right of the last '\' e.g.
    Code:
    mystring1 = 'Media Library\\Audio\\Random Playlists>>\\Albums at random>>\\Favourite album'
    mystring2 =  'Media Library\\Audio\\Random Playlists>>\\Tracks at random>>\\Rock & pop>>\\100 Rock & pop>>\\Rated at least 4*'
    
    --some function on each of the above strings returns:
    mynewstring1 = 'Favourite album'
    mynewstring2 = 'Rated at least 4*'
    Can anyone beat Rob to the answer? :wink:
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  2. #2
    Join Date
    May 2002
    Location
    CT
    Posts
    6,559

    Default

    I think I can.

    Code:
    function stripper(str,delim)
    	local i =0;
    	local lasti = nil;
    	while(i ~= nil) do
    	      lasti = i;
    	      i = string.find(str, delim, i+1);    -- find 'next' delim
    	end;
    	if (lasti == nil) return (str,nil);
    	return (string.sub(str,0,lasti-1),string.sub(str,lasti+1));
    end;
    usage:

    Code:
    mystring1 = 'Media Library\\Audio\\Random Playlists>>\\Albums at random>>\\Favourite album' ;
    path,mynewstring1 = stripper(mystring1,'\\');

  3. #3
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Should be able to do that in a single regexp though - something like :-
    Code:
    _, _, pre, post = string.find(str, '(.-)\\([^\\]+)$')
    And I assume that you meant stripping off everything to the left of the last '\'
    --Rob

  4. #4

    Default

    Perfect. Thanks guys.

    I only wish I knew how the hell you came up with '(.-)\\([^\\]+)$'

    I've read the guide but it just goes way over my head.

    Thanks anyway.
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  5. #5
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    It's not all that tricky - here's a breakdown
    Code:
    ( .- ) \\ ( [^\\]+ ) $
    Starting at the right hand side I used $ to match the end of the string
    That's preceded by ([^\\]+) - we can ignore the brackets as that just indicates that this bit is a capture. The + means that we want a 'greedy' match of at least 1 character (I think that's right). The [] is a character class or set of characters. ^ inside the character class means 'not' so that bit matches the longest string of characters at the end of the string that isn't a '\'
    --Rob

  6. #6
    Join Date
    Dec 2001
    Posts
    11,560

    Default

    Quote Originally Posted by rah99
    It's not all that tricky - here's a breakdown
    Code:
    ( .- ) \\ ( [^\\]+ ) $
    Starting at the right hand side I used $ to match the end of the string
    That's preceded by ([^\\]+) - we can ignore the brackets as that just indicates that this bit is a capture. The + means that we want a 'greedy' match of at least 1 character (I think that's right). The [] is a character class or set of characters. ^ inside the character class means 'not' so that bit matches the longest string of characters at the end of the string that isn't a '\'
    Rob, I would rather reread my book on 6502 assembly programming then learn that stuff :lol:

  7. #7
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    LOL
    --Rob

  8. #8

    Default

    I think completely lost would be a good description of how I felt after reading that explanation but thanks for trying!
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  9. #9

    Default

    Hey it's surprising what you can learn when you sober up and check the manual. I've just been reading about patterns and captures and now I actually understand what all those (*%^.+% etc mean!

    It's not as hard as it looks.
    JLee
    OpusNR & JLee CCF/GML available in the downloads section.

  10. #10
    Join Date
    May 2004
    Location
    Cardigan, UK
    Posts
    9,278

    Default

    Incidentally you can also use the following which is a slightly simpler pattern
    Code:
    _, _, pre, post = string.find(str, '(.*)\\(.-)$')
    Note that the first capture is a 'greedy' one
    --Rob

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •