简体   繁体   中英

How to extract a string between the first two and last two characters in java?

Edited: I want to extract the string between two special characters at the beginning and end.

Input can be one of the following:
{[TestString]} {TestString} [TestString] [TestString]} {[TestString

Expected Output: TestString

Here the special characters { , [ , ] , } are optional. The input string can be with/without these special chars at the beginning and end.

Using this regex below in Pattern.compile() , I am not getting the intended result.

(?=\[|\{)(.*?)(?=\]|\})

You used a lookahead assertion where you should have used lookbehind. Character classes make sure that either of [{ and ]} will match:

(?<=[{\[])(.*?)(?=[]}])

This would return TestString

Test it live on regex101.com . Be aware that you need to double the backslashes if you want to build the regex using a Java String (check the Code Generator feature of Regex101).

You may use this regex:

[\[{]*([^\]\[{}]+)[]}]*

RegEx Demo

RegEx Details:

  • [\[{]* : Match 0 or more of [ or { characters
  • ([^\]\[{}]+) : Match 1 or more of any characters that are not [ , ] , { and } . Capture this in group #1
  • []}]* : Match 0 or more of ] or } characters

Demo using Java:

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

public class Main {
    public static void main(String[] args) {
        //Test
        
        Stream.of(
                "{[TestString]}", 
                "{TestString}", 
                "[TestString]", 
                "[TestString]}", 
                "{[TestString"
        ).forEach(s -> System.out.println(s + " => " + getToken(s)));       
    }
    static String getToken(String s) {
        Matcher matcher = Pattern.compile("[\\[{]*([^\\]\\[{}]+)[]}]*").matcher(s);
        String token="";
        if(matcher.find()) {
            token = matcher.group(1);
        }
        return token;
    }
}

Output:

{[TestString]} => TestString
{TestString} => TestString
[TestString] => TestString
[TestString]} => TestString
{[TestString => TestString

Something like ^\{?\[?(.*?)\]?\}?$ , that is

(?x) # enable comments  - just for this description
^    # start
\{?  # optional {
\[?  # optional [
(    # start group
.*?  # anything reluctant (try as less as possible)
)    # group end
\]?  # optional ]
\}?  # optional }
$    # end

see regexplanet press the green Java field to see it running

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM