Using regex for two delimiters to extract strings

Related searches

The question How to delete the words between two delimiters? was really helpful to me.

So I have a string like this:

string = $blabla$blav:1085$350_X[7:0]

I am trying to remove whatever is inside the '[]' to make the whole thing be $blabla$blav:1085$350_X.

I tried all these:

re.sub('[[^]]+]', '', string)
re.sub(r'[.+?]', '', string)
re.sub('[.*?]', '', string)

Any method to do it with regex involving one step please.

Also, I need to capture that string[7:0] for later use.

Try the regex \[([0-9]+\:[0-9]+)\]$. It matches [X:Y] where X and Y are numbers and the whole thing is at the end of a string. There is only one group in the regex that returns the two numbers X:Y without the [ and ]

Use this to replace the string:

import re
re.sub('\[([0-9]+\:[0-9]+)\]$', '', string)

You can use this \[([0-9]+)\:([0-9]+)\]$ to match the two numbers in two groups.

numbersRegex ='\[([0-9]+)\:([0-9]+)\]$', string)
number1 =
number2 =
bothNumbers =

It is important to use regex instead of just string indexes in case the numbers are two or more digits. Otherwise, it is fine to indices.

If the [X:Y] is not at the end of the string, just remove the $ from the regex.

You can use this website and paste the regex there. It provides explanation and a text field to test it.

Select all characters between, Regular expression tester with syntax highlighting, PHP / PCRE & JS Support, contextual help, cheat sheet, reference, and Groups multiple tokens together and creates a capture group for extracting a substring or using a backreference. PCRE allows two types of delimiters: matched delimiters and bracket-style delimiters. Matched delimiters make use of a single character's pair, while bracket-style delimiters make use of a couple of characters which represents an opening and closing pair.

string= '$blabla$blav:1085$350_X[7:0]'

cut_string = string.split('[')[0] # = '$blabla$blav:1085$350_X'

bracket_data = string.split('[')[1].replace(']', '') # = '7:0'

Dirty, but it just werks.

Python Exercises: Split a string with multiple delimiters, Python Regular Expression: Exercise-47 with Solution. Write a Python program to split a string with multiple delimiters. Note : A delimiter is a  Simple, free and easy to use online tool that extracts regex matches from a string. No ads, popups or nonsense, just a regex matches extractor. Load a string, get matches. Simple, free and easy to use online tool that extracts regex matches from a string. No ads, popups or nonsense, just a regex matches extractor. Load a string, get matches.

You can use rsplit with maxsplit=1 to make sure it only split on last [,

string = "$blabla$blav:1085$350_X[7:0]"
s_string = string.rsplit('[', maxsplit=1)

left = s_string[0]
right = "[" + s_string[-1]

# output


If you must use regex, then try positive lookahead to match last occurrence of [,

import re

string = "$blabla$blav:1085$350_X[7:0]"
regex = r'(^.*(?=\[))(.*)'
ss = re.match(regex, string)

left =
right =


# output


Split strings at delimiters - MATLAB split, If str is a string array or cell array of character vectors, and has multiple elements, then each element must Split String at Delimiter and Join with New Delimiter. Regular Expression to find a string included between two characters while EXCLUDING the delimiters -

about_Split, Explains how to use the Split operator to split one or more strings into a regular expression in the delimiter, rather than a simple character. If you submit multiple strings, all the strings are split using the same delimiter rules  Regular expression to split a string into multiple strings based on a delimiter. and replace first two commands i.e the regular expression to extract Agent

How to get the substring between two markers in Python, Extracting the substring between two known marker strings returns the characters between the In the latter case, use to get the desired substring. Further reading Regular expressions are powerful tools for parsing strings. After a quick glance at Get-Help about_Split I moved onto using the -split operator. First, what happens when we split our string on ‘[‘? Have to use \[ because it takes regex! Right, well we only want the 2nd result of each so let’s grab only that! 1 = 2 remember? Can we split that string on ‘]’ to get the rest?

use regex to remove a number from a string 2 Answers . How to edit my regex to extract a variable string that may have either dashes or spaces? 1 Answer . How to extract the count of events to look for one or two specific strings and ignore the rest of the text in the fields? 3 Answers

  • please check my updated answer.
  • if the string is string = $blabla$blav:1085$350_X[12:0] then it is not working ..what to do ?
  • Hey! there was a mistake in my regex, I've fixed it now. Should work with double and multiple digit numbers. The + sign should have been placed after the [. Please use to check your regexes. It provides an explanation of the syntax.
  • What happens if there is another [ or ] in the string?
  • Thanks, I solved it this way too but I wanted to solve it with regex in one line preferably .
  • Check my answer then ;)
  • Thanks but their position not always gonna be same.
  • @zaki, i have also added regex answer if you like