Write a program to get a substring of a given string between two specified positions

Given a string str, the task is to extract the substrings present between two delimiters, i.e. ‘[‘ and ‘]’.

Examples:

Input: str = “[This is a string to be extracted]”
Output: This is a string to be extracted
Explanation: The square brackets ‘[‘ and ‘]’ serve as delimiters in the given string.

Input: str= “[This is first] ignored text [This is second]”
Output: This is firstThis is second

Explanation: The square brackets ‘[‘ and ‘]’ serve as delimiters in the given string.

Stack-based Approach: Iterate over the characters of the string and insert the index of every ‘[‘ encountered into the stack. For every ‘]’ encountered, simply pop the index stored at the top of the stack and print the substring lying in between.



Below is the implementation of the above approach

void printSubsInDelimiters(string str)

    for (int i = 0; i < str.size(); i++) {

        else if (str[i] == ']' && !dels.empty()) {

    string str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

static void printSubsInDelimiters(String str)

    Stack<Integer> dels = new Stack<Integer>();

    for(int i = 0; i < str.length(); i++)

        if (str.charAt(i) == '[')

        else if (str.charAt(i) == ']' &&

            String ans = str.substring(

            System.out.print(ans + "\n");

public static void main(String[] args)

    String str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

def printSubsInDelimiters(string) :

    for i in range(len(string)):

        elif (string[i] == ']' and len(dels) != 0) :

            ans = string[pos + 1 : pos + 1 + length];

if __name__ == "__main__" :

    string = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(string);

using System.Collections;

static void printSubsInDelimiters(string str)

    Stack dels = new Stack();

    for(int i = 0; i < str.Length; i++)

        else if (str[i] == ']' && dels.Count > 0)

            int pos = (int)dels.Peek();

            string ans = str.Substring(

    string str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

    function printSubsInDelimiters(str)

        for(let i = 0; i < str.length; i++)

            else if ((str[i] == ']') &&

                let pos = dels[dels.length - 1];

                    ans = str.substring(pos + 1,

                    ans = str.substring(pos + 1,

                document.write(ans + "</br>");

    let str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

Output:  This is first This is second

Time Complexity: O(N)
Auxiliary Space: O(N)

Space-Efficient Approach: The idea is to use Regular Expressions to solve this problem. Create a regular expression to extract the string between two delimiters as regex = “\\[(.*?)\\]” and match the given string with the Regular Expression. Print the subsequence formed.

Below is the implementation of the above approach:

void printSubsInDelimiters(string str)

    const regex pattern("\\[(.*?)\\]");

    for(sregex_iterator it = sregex_iterator(

        str.begin(), str.end(), pattern);

        it != sregex_iterator(); it++)

        cout << match.str(1) << endl;

    string str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

import java.util.regex.*;

public static void printSubsInDelimiters(String str)

    String regex = "\\[(.*?)\\]";

    Pattern p = Pattern.compile(regex);

    Matcher m = p.matcher(str);

        System.out.println(m.group(1));

public static void main(String args[])

    String str = "[This is first] ignored text [This is second]";

    printSubsInDelimiters(str);

def printSubsInDelimiters(str):

    matches = re.findall(regex, str)

str = "[This is first] ignored text [This is second]"

printSubsInDelimiters(str)

Output:  This is first This is second

Time Complexity: O(N)
Auxiliary Space: O(1)


Practice Tags :