Harish
Harish

Reputation: 726

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.

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

Upvotes: 0

Views: 411

Answers (3)

user15793316
user15793316

Reputation:

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

Upvotes: 1

anubhava
anubhava

Reputation: 786091

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

Upvotes: 3

Tim Pietzcker
Tim Pietzcker

Reputation: 336468

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).

Upvotes: 5

Related Questions