问题描述:

I need to fetch the data from a PDF file and use it as a test data to test an application using Selenium Webdriver.

I stored the pdf data in a variable and fetching the required data one by one.

Now the problem I am facing here is that the below data can be multiple times in that file, only the data in bold will be changing. I need to fetch those bold values.

Airline Record Locator JUI1244

Airline XXX Airlines

Flight HU124

Origin CCC CST Airport

Destination XXX YYY, International

Departing 07:35 PM

Arriving 09:20 AM / 24 Oct 2015

Departure Terminal Terminal C

Arrival Terminal Terminal 1

I tried Substring to get the value but it is always giving me the first instance value. So, I tried to use Regex and was able to get the 'Airline Record Locator' line value using below regex

 Matcher m1 = Pattern.compile("(?m)(?:^Airline Record Locator )([a-zA-Z]+([0-9]+).*)").matcher(data);

while(m1.find()) {

System.out.println(m1.group(1));

}

Above regex doesn't fetch data from 'Airline' line, so for that I used the below regex.

 Pattern.compile("(?m)(?:^Airline )([A-Z]*.*?[a-z]$)").matcher(data);

But both the above regex code is not fetching the values from other fields like 'Departing', 'Arrival Terminal', 'Departing Terminal' etc.. line data.

My question

  1. How to fetch the values from above fields 'Departing', 'Arrival Terminal', 'Departing Terminal' etc. ?
  2. Do I need to use different regex patterns to fetch the values from different fields ? Or is there any way I can use a single regex pattern to fetch the data from all lines (Obviously the String can be different but can we have the same regex pattern ) ?

I am new to Regex and any help is appreciated. Thanks.

网友答案:
public static void test(String totalstring,String valueof){

        String arr[]=totalstring.split(valueof);

        System.out.println(arr[1].trim());

        }

//pass the line and the String for which u want the value ex : Depature
String totalstring ="Departure Terminal Terminal C";

String valueof="Departure";

test(totalstring,valueof);

Hope this helps you.Kindly get back if you need any further help

网友答案:

I think you can use this solution by regex:

/(.*?)((Airline Record Locator\s+(.*))|(Airline\s+(.*))|(Flight\s+(.*))|(Origin\s+(.*))|(Destination\s+(.*))|(Departing\s+(.*))|(Arriving\s+(.*))|(Departure Terminal\s+(.*))|(Arrival Terminal\s+(.*)))\s*/ig

That substitutions will give you values like \4, \6, \8, ... and \20

[Regex Demo]

相关阅读:
Top