senzacionale senzacionale - 11 months ago 77
Java Question

find substrings inside string

How can i find substrings inside string and then remember and delete it when i found it.

EXAMPLE:

select * from (select a.iid_organizacijske_enote,
a.sifra_organizacijske_enote "Sifra OE",
a.naziv_organizacijske_enote "Naziv OE",
a.tip_organizacijske_enote "Tip OE"


I would like to get all word inside " ", so


  • Sifra OE

  • Naziv OE

  • TIP OE



and return

select * from (select a.iid_organizacijske_enote,
a.sifra_organizacijske_enote,
a.naziv_organizacijske_enote,
a.tip_organizacijske_enote


i try with regex,
indexOf()
but no one works ok

Answer Source

Consider using regex with capturing groups. With Java's Matcher class, you can find the first match, and then use replaceFirst(String).

--EDIT--

example (not efficient for long inputs):

String in = "hello \"there\", \"friend!\"";
Pattern p = Pattern.compile("\\\"([^\"]*)\\\"");
Matcher m = p.matcher(in);
while(m.find()){
    System.out.println(m.group(1));
    in = m.replaceFirst("");
    m = p.matcher(in);
}
System.out.println(in);