简体   繁体   中英

Extracting a part of string in java

I am having the following data in a text file:

 proc sort data=oneplan.cust_duplicates(dbindex=yes) out=mibatch.cust_duplicates;
    from oneplan.fsa_authorised_agencies (dbindex=yes) 
    set oneplan.fsa_agency_permissions(where=(regulated_activity in ('103','106','107','108')));
    set oneplan.customer_flags(where=(flag_id in(54,14,34)));
    from oneplan.document_history (dbindex=yes) 
    set oneplan.product_references;
  proc sort data=oneplan.fast_track_log(where=(upcase(business_entry)="INCOME VERIFICATION FAST TRACKED"))   out=fast_track_log;
    set oneplan.product_characteristics(rename=(value=filler)where=(characteristic_type="OFFS" ));
    set oneplan.mtg_offset_benefits (where=(modified_date between &extractdate and &batchcutoff) 
      from oneplan.mtg_payment_options a;
    from oneplan.acc_retention_risk acr;
    from oneplan.acc_retention_risk_hist acr;
    from oneplan.account_repay_veh rv;
    from oneplan.repay_vehicle_map rvm;
    from oneplan.frozen_accounts as fa left join mibatch.accounts as a

Now i need to fetch the part from each line which starts with oneplan. and ends with a space.

Also if the line has two oneplan. then each must be extracted separately.

Example: agtut oneplan.m htyutu oneplan.j hgyut

i need the output as: oneplan.m and oneplan.j

kindly give me suggestions in doing this please...

You could use a regex like:

String text = ....
String regex = "(oneplan\\.\\w)\\w+\\s+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
while(matcher.find()) {
   System.out.println(matcher.group(1));
}

Will print the results:

oneplan.f
oneplan.d
oneplan.m
oneplan.m
oneplan.a
oneplan.a
oneplan.a
oneplan.r
oneplan.f

If you instead want the entire word you can use matcher.group() in combination with the regex:

"oneplan\\.\\w+\\s+"

您可以从BufferedReader#readLineString#startsWith String#endsWith开始。

看一下各种String.indexOf()String.substring()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM