Extract String Between Two Strings in Java

Java Regex Extract String between two Strings

I write test as in Extract string between two strings in java and this is working. I think Your input string don't matches:

 @Test
public void regex() {
String str = "Nom for 3 Oscar, dom for 234235 Oscars";
Pattern pattern = Pattern.compile("for(.*?)Oscar");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}

Output:

    3 
234235

After my answer You edited Your question and I see, in Your input String "oscar" starts with lowecase "o", in Pattern with uppercase "O".

How to extract a string between two delimiters

If you have just a pair of brackets ( [] ) in your string, you can use indexOf():

String str = "ABC[ This is the text to be extracted ]";    
String result = str.substring(str.indexOf("[") + 1, str.indexOf("]"));

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

You can construct the regex to do this for you:

// pattern1 and pattern2 are String objects
String regexString = Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2);

This will treat the pattern1 and pattern2 as literal text, and the text in between the patterns is captured in the first capturing group. You can remove Pattern.quote() if you want to use regex, but I don't guarantee anything if you do that.

You can add some customization of how the match should occurs by adding flags to the regexString.

  • If you want Unicode-aware case-insensitive matching, then add (?iu) at the beginning of regexString, or supply Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE flag to Pattern.compile method.
  • If you want to capture the content even if the two delimiting strings appear across lines, then add (?s) before (.*?), i.e. "(?s)(.*?)", or supply Pattern.DOTALL flag to Pattern.compile method.

Then compile the regex, obtain a Matcher object, iterate through the matches and save them into a List (or any Collection, it's up to you).

Pattern pattern = Pattern.compile(regexString);
// text contains the full text that you want to extract data
Matcher matcher = pattern.matcher(text);

while (matcher.find()) {
String textInBetween = matcher.group(1); // Since (.*?) is capturing group 1
// You can insert match into a 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 that if you search for the text between foo and bar in this input foo text foo text bar text bar with the method above, you will get one match, which is  text foo text .

How to extract string between two strings using java Pattern

If you want to use a regular expression, a simple way would be:

return subscriber.replaceAll("/.*/([^/]*)/.*", "$1");
  • /.*/ is for the /subscription/ bit
  • ([^/]*) a capturing group that matches all characters until the next /
  • /.* is for the /subscribe bit

And the second argument of replaceAll says that we want to keep the first group.

You can use a Pattern to improve efficiency by compiling the expression:

Pattern p = Pattern.compile("/.*/([^/]*)/.*"); ///store it outside the method to reuse it

Matcher m = p.matcher(subscriber);
if (m.find()) return m.group(1);
else return "not found";

R - Extract String between two strings

Here's a regex method, though as I mentioned in comments I'd strongly recommend using, e.g., the jsonlite package instead.

# input:
x = c('> }],"Country":"United States",',
'> }],"Country":"China",')

library(stringr)
result = str_extract(x, pattern = '(?<=Country":")[^,]+(?=",)')
result
# [1] "United States" "China"

Explanation:

  • (?<=...) is the look-behind pattern. So we're looking behind (before) the match for Country":".
  • [^"]+ is our main pattern - ^ in brackets is "not", so we're looking for any character that is not a ". And + is the quantifier, so one or more non-" characters.
  • (?=...) is the look-ahead pattern. So we're looking after the match for ","

Extract a complex String from between two Strings

Pattern p = Pattern.compile("\\[Text:(.*?)\\]");
Matcher m = p.matcher("[Qual:3] [Text:PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1] [Elem:123]");
m.find();
System.out.println(m.group(1));

Gives:

PIX 1252471471953/YHYF/PPP121.40/10RTY10/NOLXX08X1

The \\[ and \\] are to escape the brackets, which are special characters in regexes. The .*? is a non-greedy quantifier, so it stops gobbling up characters when it reaches the closing bracket. This part of the regex is given inside a capturing group (), which you can access with m.group(1).

Extract string between two words in Java

try changing:

Pattern pattern = Pattern.compile("<cases>(.*?)</cases>");

into

Pattern pattern = Pattern.compile("<cases>(.*?)</cases>", Pattern.DOTALL);

btw, if it was a well-formed xml document, use parser instead of regex to handle it.

BigQuery Regex to extract string between two substrings

use regexp_extract(col, r"&q;Stockcode&q;:([^/$]*?),&q;.*")

if applied to sample data in your question - output is

Sample Image



Related Topics



Leave a reply



Submit