Vinu Vish Vinu Vish - 5 months ago 11
Java Question

How to pull out specific lines using regex

how can i pull out only Scenario and Sections lines from this story file using regex or tokanizer

Scenario: 01. User submits trade with empty value for Primary Exchange MIC and with Trade Venue
(Sections Vol 02 - TM (v2.01) 6.2.5.1, 6.2.5.2, 6.2.5.3 TE_15)

**Given registered user WebAdmin1 is available
And CCP Admin user generates trade for QAFR007 via GUI
And sets "tradingVenue" as "LSE" is entered via GUI
And value for primaryExchangeMic not entered in GUI
When user WebAdmin1 submits trade via GUI
Then validation message should show as Trade submitted successfully
And trade details should be displayed in the query window
And add column Primary Exchange MIC to query window
And Primary Exchange MIC should display as 1234
And Trade Status should display as New


And the output should be like this

Scenario: 01. User submits trade with empty value for Primary Exchange MIC and with Trade Venue
(Sections Vol 02 - TM (v2.01) 6.2.5.1, 6.2.5.2, 6.2.5.3 TE_15)

Scenario: 02. User submits trade with empty value for Primary Exchange MIC and with Trade Venue
(Sections Vol 02 - TM (v2.01) 6.2.5.1, 6.2.5.2, 6.2.5.3 TE_15)

Answer

If I understand the issue correctly, you want to pull out the following lines.

Scenario: 01. User submits trade with empty value for Primary Exchange MIC and with Trade Venue
(Sections Vol 02 - TM (v2.01) 6.2.5.1, 6.2.5.2, 6.2.5.3 TE_15)

Scenario: 02. User submits trade with empty value for Primary Exchange MIC and with Trade Venue
(Sections Vol 02 - TM (v2.01) 6.2.5.1, 6.2.5.2, 6.2.5.3 TE_15)

In order to do this, I recommend using RegEx to pull out all sections of it, then work on each individual section to to strip further. Here is an example that gets the main lines. ( data = String containing text above)

try {
    Pattern regex = Pattern.compile("^(Scenario:.*?\\(Sections.*?\\))$", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL | Pattern.MULTILINE);
    Matcher m = regex.matcher(data);
    while (m.find()) {
        String lineToProcess = m.group(1);
    } 
} catch (PatternSyntaxException e) {
    // handle error.
}