Java - Best way to grab ALL Strings between two Strings? (regex?) -


this question has been bugging me long time i'm looking efficient way grab strings between 2 strings.

the way have been doing many months through using bunch of temporary indices, strings, substrings, , it's messy. (why java not have native method such string substring(string start, string end)?

say have string:

abcabc [pattern1]foo[pattern2] abcdefg [pattern1]bar[pattern2] morestuff

the end goal output foo , bar. (and later added jlist)

i've been trying incorporate regex in .split() haven't been successful. i've tried syntax using *'s , .'s don't think it's quite intention since .split() takes 1 argument split against.

otherwise think way use pattern , matcher classes? i'm fuzzy on appropriate procedure.

you can construct regex you:

// pattern1 , pattern2 string objects string regexstring = pattern.quote(pattern1) + "(.*?)" + pattern.quote(pattern2); 

this treat pattern1 , pattern2 literal text, , text in between patterns captured in first capturing group. can remove pattern.quote() if want use regex, don't guarantee if that.

you can add customization of how match should occurs adding flags regexstring.

  • if want unicode-aware case-insensitive matching, add (?iu) @ beginning of regexstring, or supply pattern.case_insensitive | pattern.unicode_case flag pattern.compile method.
  • if want capture content if 2 delimiting strings appear across lines, add (?s) before (.*?), i.e. "(?s)(.*?)", or supply pattern.dotall flag pattern.compile method.

then compile regex, obtain matcher object, iterate through matches , save them list (or collection, it's you).

pattern pattern = pattern.compile(regexstring); // text contains full text want extract data matcher matcher = pattern.matcher(text);  while (matcher.find()) {   string textinbetween = matcher.group(1); // since (.*?) capturing group 1   // can insert match list/collection here } 

testing code:

string pattern1 = "hgb"; string pattern2 = "|"; string text = "sdfjsdkhfkjsdf hgb sdjfkhsdkfsdf |sdfjksdhfjksd sdf sdkjfhsdkf | sdkjfh hgb sdkjfdshfks|";  pattern p = pattern.compile(pattern.quote(pattern1) + "(.*?)" + pattern.quote(pattern2)); matcher m = p.matcher(text); while (m.find()) {   system.out.println(m.group(1)); } 

do note if search text between foo , bar in input foo text foo text bar text bar method above, 1 match,  text foo text .


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -