How to extract a substring using regex

前端 未结 14 1284
暖寄归人
暖寄归人 2020-11-22 13:37

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

相关标签:
14条回答
  • 2020-11-22 14:19

    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_.+-]+@[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);
    
    0 讨论(0)
  • 2020-11-22 14:20

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

    See Live Demo

    0 讨论(0)
  • 2020-11-22 14:22

    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 "'")
    
    0 讨论(0)
  • 2020-11-22 14:23

    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
    
    0 讨论(0)
  • 2020-11-22 14:24

    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.

    0 讨论(0)
  • 2020-11-22 14:25

    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, "'", "'");
    
    0 讨论(0)
提交回复
热议问题