By asdasd


2011-01-11 20:22:49 8 Comments

I have a string that has two single quotes in it, the ' character. In between the single quotes is the data I want.

How can I write a regex to extract "the data i want" from the following text?

mydata = "some string with 'the data i want' inside";

13 comments

@Arindam 2020-06-10 13:11:45

Some how the group(1) didnt work for me. I used group(0) to find the url version.

Pattern urlVersionPattern = Pattern.compile("\\/v[0-9][a-z]{0,1}\\/");
Matcher m = urlVersionPattern.matcher(url);
if (m.find()) { 
    return StringUtils.substringBetween(m.group(0), "/", "/");
}
return "v0";

@Ganesh 2020-02-27 10:39:45

add apache.commons dependency on your pom.xml

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

And below code works.

StringUtils.substringBetween(String mydata, String "'", String "'")

@Nouh Mohamed 2020-02-09 20:51:16

you can use this i use while loop to store all matches substring in the array if you use

if (matcher.find()) { System.out.println(matcher.group(1)); }

you will get on matches substring so you can use this to get all matches substring

Matcher m = Pattern.compile("[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+").matcher(text);
   // Matcher  mat = pattern.matcher(text);
    ArrayList<String>matchesEmail = new ArrayList<>();
        while (m.find()){
            String s = m.group();
            if(!matchesEmail.contains(s))
                matchesEmail.add(s);
        }

    Log.d(TAG, "emails: "+matchesEmail);

@Memin 2019-07-26 15:11:16

Apache Commons Lang provides a host of helper utilities for the java.lang API, most notably String manipulation methods. In your case, the start and end substrings are the same, so just call the following function.

StringUtils.substringBetween(String str, String tag)

Gets the String that is nested in between two instances of the same String.

If the start and the end substrings are different then use the following overloaded method.

StringUtils.substringBetween(String str, String open, String close)

Gets the String that is nested in between two Strings.

If you want all instances of the matching substrings, then use,

StringUtils.substringsBetween(String str, String open, String close)

Searches a String for substrings delimited by a start and end tag, returning all matching substrings in an array.

For the example in question to get all instances of the matching substring

String[] results = StringUtils.substringsBetween(mydata, "'", "'");

@ZehnVon12 2017-09-13 08:28:21

String dataIWant = mydata.replaceFirst(".*'(.*?)'.*", "$1");

@ZehnVon12 2017-08-16 13:15:17

String dataIWant = mydata.split("'")[1];

See Live Demo

@Bohemian 2017-01-14 23:29:13

There's a simple one-liner for this:

String target = myData.replaceAll("[^']*(?:'(.*?)')?.*", "$1");

By making the matching group optional, this also caters for quotes not being found by returning a blank in that case.

See live demo.

@Mark Byers 2011-01-11 20:27:45

Assuming you want the part between single quotes, use this regular expression with a Matcher:

"'(.*?)'"

Example:

String mydata = "some string with 'the data i want' inside";
Pattern pattern = Pattern.compile("'(.*?)'");
Matcher matcher = pattern.matcher(mydata);
if (matcher.find())
{
    System.out.println(matcher.group(1));
}

Result:

the data i want

@Mihai Toader 2011-01-11 20:28:54

damn .. i always forget about the non greedy modifier :(

@OneWorld 2012-08-07 16:25:42

replace the "if" with a "while" when you expect more than one occurences

@rexford 2014-07-31 14:03:37

mind that matcher.find() is needed for this code sample to work. failing to call this method will result in a "No match found" exception when matcher.group(1) is called.

@mFontoura 2015-01-15 19:29:16

If you want the first result it should be ".group(0)" and not ".group(1)".

@tagy22 2015-02-19 14:34:57

@mFontoura group(0) would return the complete match with the outer ' '. group(1) returns what is in-between the ' ' without the ' ' themselves.

@Larry 2015-05-13 12:25:24

why does Mark use the question mark in this case? doesn't the .* match 0 or more anyway? So if there was an empty string between the two quotations it would match nonetheless?

@Giuseppe Bianco 2016-01-21 12:31:07

This code works well, but in the result the delimiters (') is included. How to get the substring without the delimiters?

@Donovan Tan 2016-01-22 06:41:54

@Mark Byers Hi, can you take a look at this question? stackoverflow.com/questions/34938232/…

@Boo Radley 2016-04-26 16:29:18

This answer is slightly misleading, as the code provided returns 'the data i want' instead of the data i want. If you want to remove the single quotes you should print matcher.group(1) instead.

@holmis83 2016-05-06 11:09:49

@BooRadley The answer was correct from the beginning, I made rollback.

@Timekiller 2016-09-12 14:08:41

@Larry this is a late reply, but ? in this case is non-greedy modifier, so that for this 'is' my 'data' with quotes it would stop early and return is instead of matching as many characters as possible and return is' my 'data, which is the default behavior.

@mikato 2017-04-05 15:24:22

@tagy22 How does that work with the group(1) returning the value between the single quotes? How would I do that for something else, such as [blah]? Is it going by the parentheses?

@Beothorn 2013-03-13 20:37:23

You don't need regex for this.

Add apache commons lang to your project (http://commons.apache.org/proper/commons-lang/), then use:

String dataYouWant = StringUtils.substringBetween(mydata, "'");

@Beothorn 2015-04-13 14:41:38

You have to take into account how your software will be distributed. If it is something like a webstart it's not wise to add Apache commons only to use this one functionality. But maybe it isn't. Besides Apache commons has a lot more to offer. Even tough it's good to know regex, you have to be carefull on when to use it. Regex can be really hard to read, write and debug. Given some context using this could be the better solution.

@Gábor Nagy 2016-09-14 11:58:16

Sometimes StringUtils is already there, in those cases this solution is much cleaner and readable.

@prayagupd 2017-03-01 20:38:22

Its like buying a car to travel 5 miles (when you are traveling only once in a year).

@burakhan alkan 2017-09-19 10:20:55

While substring looks for a specific string or value, regex looks for a format. It's more and more dynamic. You need regex, if you are looking for a pattern instead of a special value.

@Daniel C. Sobral 2011-01-11 22:32:48

In Scala,

val ticks = "'([^']*)'".r

ticks findFirstIn mydata match {
    case Some(ticks(inside)) => println(inside)
    case _ => println("nothing")
}

for (ticks(inside) <- ticks findAllIn mydata) println(inside) // multiple matches

val Some(ticks(inside)) = ticks findFirstIn mydata // may throw exception

val ticks = ".*'([^']*)'.*".r    
val ticks(inside) = mydata // safe, shorter, only gets the first set of ticks

@Debilski 2011-01-11 21:03:46

Because you also ticked Scala, a solution without regex which easily deals with multiple quoted strings:

val text = "some string with 'the data i want' inside 'and even more data'"
text.split("'").zipWithIndex.filter(_._2 % 2 != 0).map(_._1)

res: Array[java.lang.String] = Array(the data i want, and even more data)

@prayagupd 2017-03-01 20:42:49

So readable solution, thats why people love scala I belive :)

@ArtOfWarfare 2017-04-10 17:05:36

Why not just .split('\'').get(2) or something to that extent in Java? I think you may need to get a brain scan if you think that's a readable solution - it looks like someone was trying to do some code golf to me.

@Sean McEligot 2011-01-11 20:40:47

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile(".*'([^']*)'.*");
        String mydata = "some string with 'the data i want' inside";

        Matcher matcher = pattern.matcher(mydata);
        if(matcher.matches()) {
            System.out.println(matcher.group(1));
        }

    }
}

@nclord 2016-05-13 14:49:20

System.out.println(matcher.group(0)); <--- Zero based index

@Apriori 2017-04-18 06:48:28

No. group(0) has special meaning, capturing groups start at index group(1) (i.e. group(1) is correct in the answer). "Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern" - Source: docs.oracle.com/javase/8/docs/api/java/util/regex/…

@Mihai Toader 2011-01-11 20:26:49

as in javascript:

mydata.match(/'([^']+)'/)[1]

the actual regexp is: /'([^']+)'/

if you use the non greedy modifier (as per another post) it's like this:

mydata.match(/'(.*?)'/)[1]

it is cleaner.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How do I get a substring of a string in Python?

33 Answered Questions

[SOLVED] startsWith() and endsWith() functions in PHP

  • 2009-05-07 12:14:27
  • Click Upvote
  • 848160 View
  • 1491 Score
  • 33 Answer
  • Tags:   php string

3 Answered Questions

26 Answered Questions

[SOLVED] How to check if a string contains a substring in Bash

21 Answered Questions

[SOLVED] Extract substring in Bash

35 Answered Questions

[SOLVED] How to split a string in Java

  • 2010-08-14 03:01:53
  • riyana
  • 3915621 View
  • 1653 Score
  • 35 Answer
  • Tags:   java string split

31 Answered Questions

[SOLVED] endsWith in JavaScript

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

20 Answered Questions

[SOLVED] How do you use a variable in a regular expression?

  • 2009-01-30 00:11:05
  • JC Grubbs
  • 778479 View
  • 1399 Score
  • 20 Answer
  • Tags:   javascript regex

Sponsored Content