#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    11
    Rep Power
    0

    Is this possible with backreferencing?


    I have the following string and I need to come up with a regex that will match the first appearance of each new segment number:

    SegmentNumber: 23,Mu: 11,0,0,0,0,-10,-10,SegmentNumber: 23,Mu: 11,-1,1,-1,1,-9,-11,-9,SegmentNumber: 24,Mu: 10,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,SegmentNumber: 23,Mu: 11,-1,1,-1,1,-11,-9,-11,-9,-11,-9,-11,-9,-11,-9,-11,-9,-11,-10,-11,-9,-11,-9,SegmentNumber: 24,Mu: 10,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,SegmentNumber: 25,Mu: 11,-8,-8,-8,-8,-8,-8,-8,-8,-8

    I have underlined where I want the regex to match.

    Any ideas? I've tried a few different approaches none of which have been successful so far.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    11
    Rep Power
    0
    For now I am looping this regex:

    Code:
    "(SegmentNumber:\s" & j & ",).*?(?=,Se)"
    Where j goes to the highest possible segment number for any situation.

    However, this way causes the regex to run many times needlessly, so if there is a way to do it without a loop, I would like to use that instead.
  4. #3
  5. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    NO, that is not something regex can do: regex does not have a notion of numerical values: everything is just text. You can't say, "match this back reference and add one to it".

    But instead of matching on all the text every time, just split the text and after that, loop through all your tokens. Something like this:

    java Code:
    public class Main { 
        public static void main(String[] args)  {
            String text = "SegmentNumber: 23,Mu: 11,0,0,0,0,-10,-10,SegmentNumber: "+
                    "23,Mu: 11,-1,1,-1,1,-9,-11,-9,SegmentNumber: 24,Mu: 10,-5,-5,-"+
                    "5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,Seg"+
                    "mentNumber: 23,Mu: 11,-1,1,-1,1,-11,-9,-11,-9,-11,-9,-11,-9,-1"+
                    "1,-9,-11,-9,-11,-10,-11,-9,-11,-9,SegmentNumber: 24,Mu: 10,-5,"+
                    "-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5,-5"+
                    ",-5,-5,SegmentNumber: 25,Mu: 11,-8,-8,-8,-8,-8,-8,-8,-8,-8";
            String[] tokens = text.split("(?!^)(?=SegmentNumber)");
            int current = Integer.parseInt(tokens[0].replaceAll("SegmentNumber:\\s+(\\d+).*", "$1"));
            System.out.println(tokens[0]);
            for(int i = 1; i < tokens.length; i++) {
                if(tokens[i].indexOf("SegmentNumber: "+(current+1)) == -1) continue;
                System.out.println(tokens[i]);
                current++;
            }
        } 
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    11
    Rep Power
    0
    Ok, thanks. I already have it doing something like that.
  8. #5
  9. No Profile Picture
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by cwilson
    Ok, thanks. I already have it doing something like that.
    You're welcome.

IMN logo majestic logo threadwatch logo seochat tools logo