Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Create REGEXP matching between multiple rows

Last post 12-09-2008, 6:20 AM by EnzoValenzetti. 8 replies.
Sort Posts: Previous Next
  •  11-17-2008, 11:40 AM 48387

    Create REGEXP matching between multiple rows

    Hi,

    I'd like to create a regexp using Java that matches checking multiple rows.

     

    There is a syntax template to realize that? Is it really possible?

     

     

     

    Best regards

    Filed under:
  •  11-17-2008, 12:18 PM 48388 in reply to 48387

    Re: Create REGEXP matching between multiple rows

    More details please. Examples to illustrate what you mean might also be a good idea.

    Thanks.

  •  11-18-2008, 4:56 AM 48436 in reply to 48388

    Re: Create REGEXP matching between multiple rows

    Tipically I Have a text file structured like this:

    -----------------------------------------

    Status

    24a

    N-ROWS

    324

    23.4

    26.2

    -----------------------------------------

     

    I'd like to got first occurrence of "\d+\.\d" after keyword "Status".

    So the resulting file is:

     

    -----------------------------------------

    Status

    23.4

    -----------------------------------------

     

    Is it possible?

     

     

     

     

  •  11-18-2008, 5:33 AM 48437 in reply to 48436

    Re: Create REGEXP matching between multiple rows

    EnzoValenzetti:
    ...

    Is it possible?

    Sure, have you already tried something? Can you post that?

  •  11-24-2008, 7:30 AM 48762 in reply to 48437

    Re: Create REGEXP matching between multiple rows

    My regexp:


    Status[\r\n]+((?<=([^(\d+\.\d)][\r\n])*)(\d+\.\d))

     But I got exception -> Look-behind group does not have an obvious maximum length near index 39 that is * character behind "([^(\d+\.\d)][\r\n])" pattern .

     

     

  •  11-24-2008, 7:51 AM 48763 in reply to 48762

    Re: Create REGEXP matching between multiple rows

    EnzoValenzetti:

    My regexp:


    Status[\r\n]+((?<=([^(\d+\.\d)][\r\n])*)(\d+\.\d))

     But I got exception -> Look-behind group does not have an obvious maximum length near index 39 that is * character behind "([^(\d+\.\d)][\r\n])" pattern .

    Your flavour of regex (which is Java, I guess) does not support variable length look behinds. Try this:

    String regex = "(?si)Status.*?(\d+\.\d)";

    The first number your interested in is stored in group 1.

  •  11-24-2008, 8:34 AM 48765 in reply to 48763

    Re: Create REGEXP matching between multiple rows

    Sorry, but I don't understand,  I'm using Java. I'd like to got only :

    Status

    23.4



    Using your previous regexp I got only:

    Status
    10-days prognosis
    New Status
    FUNDAMENTAL DRIVEN CO2 INDEX FROM 1. JANUARY 2008, LAST 30 DAYS
    EUA 2008 [?/t] EEX month ahead  [?/MWh]
    EUA 2009 [?/t] UK Power Mth ahead  [?/MWh]
    EUA 2010 [?/t] Brent Mth ahead  [$/barrel]
    EUA 2011 [?/t] EUR/USD
    CER 2008 [?/t]
    CER 2009 [?/t]
    -2.6
    Status
    10-days prognosis
    New Status
    FUEL PRICE DRIVEN CO2 INDEX FROM 1. JANUARY 2008, LAST 30 DAYS
    Status
    Change
    1
    10.5

     

     

    And another question is: what does it mean  "(?si)" ?

    Kind regards

     

     

  •  11-24-2008, 9:14 AM 48766 in reply to 48765

    Re: Create REGEXP matching between multiple rows

    EnzoValenzetti:

    Sorry, but I don't understand,  I'm using Java. I'd like to got only :

    Status

    23.4

    Then use replaceAll(...) to remove all the unwanted stuff.

    EnzoValenzetti:

    Using your previous regexp I got only:

    Status
    10-days prognosis
    New Status
    FUNDAMENTAL DRIVEN CO2 INDEX FROM 1. JANUARY 2008, LAST 30 DAYS
    EUA 2008 [?/t] EEX month ahead  [?/MWh]
    EUA 2009 [?/t] UK Power Mth ahead  [?/MWh]
    EUA 2010 [?/t] Brent Mth ahead  [$/barrel]
    EUA 2011 [?/t] EUR/USD
    CER 2008 [?/t]
    CER 2009 [?/t]
    -2.6
    Status
    10-days prognosis
    New Status
    FUEL PRICE DRIVEN CO2 INDEX FROM 1. JANUARY 2008, LAST 30 DAYS
    Status
    Change
    1
    10.5

    Well, that's what you asked: match the first decimal number after the string "Status". The string "10.5" is now stored in group 1. Use replaceAll(...) and place that group in the second parameter of the replaceAll method. Try a couple of things: that's the best way to get familiarized with Java/regex.

    Run this small demo to see if you can apply it in your situation:

    String s = "a=11, b=22, c=33";
    System.out.println(s.replaceAll(".*?b=(\\d+).*", "group 1 = $1"));

    EnzoValenzetti:

    And another question is: what does it mean  "(?si)" ?

    Kind regards

    (?si) is  short for both (?i) and (?s).

    See what they mean in the Pattern API docs:

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

    Search for "(?s)" and "(?i)"

  •  12-09-2008, 6:20 AM 49250 in reply to 48766

    Re: Create REGEXP matching between multiple rows

    Thank you,but I've solved using awk, that's I think is better approach to solve through similar problems.

     

View as RSS news feed in XML