Remove Text In-Between Delimiters in a String (Using a Regex)

removing text between CLOSEST delimiters in a string- regex

This slight modification should solve your problem:

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

This bit (.*?) will match everything but will take as little as possible.

Remove text in-between delimiters in a string (using a regex?)

Simple regex would be:

string input = "Give [Me Some] Purple (And More) Elephants";
string regex = "(\\[.*\\])|(\".*\")|('.*')|(\\(.*\\))";
string output = Regex.Replace(input, regex, "");

As for doing it a custom way where you want to build up the regex you would just need to build up the parts:

('.*')  // example of the single quote check

Then have each individual regex part concatenated with an OR (the | in regex) as in my original example. Once you have your regex string built just run it once. The key is to get the regex into a single check because performing a many regex matches on one item and then iterating through a lot of items will probably see a significant decrease in performance.

In my first example that would take the place of the following line:

string input = "Give [Me Some] Purple (And More) Elephants";
string regex = "Your built up regex here";
string sOutput = Regex.Replace(input, regex, "");

I am sure someone will post a cool linq expression to build the regex based on an array of delimiter objects to match or something.

Using regex to remove text between delimiter in java

You are using greedy quantifier. Your (\\s|\\S)* will match everything till it finds the last */, till where results in the complete pattern can be successfully matched.

You can make the quantifier reluctant by adding a ? after *.

"((/\\*)(\\s|\\S)*?(\\*/))"

Also, you can simplify your regex like this: -

String next = file.replaceAll("(?s)/\\*.*?\\*/", "");

(?s) -> Is used for SingleLine matching. It is an alternative for Pattern.DOTALL to be used in String.replaceAll(). So, your dot(.) would include everything including newline. And you don't need (\\s|\\S) for that.

remove substring between delimiters FROM LEFT TO RIGHT using regex - C#

Use negation instead of the .* token. Also, place your pattern inside a verbatim string literal.

string myRegex = @"\[\[[^[|]*\|";

Ideone Demo

Exclude/remove a string between two special characters using regex

There can be many ways e.g. you can replace \w+\/ with a "". Note that \w+ means one or more word characters.

Demo:

public class Main {
public static void main(String[] args) {
String FILENAME = "Application-2.0.2-bug/TEST-1.0.0.zip";
FILENAME = FILENAME.replaceAll("\\w+\\/", "");
System.out.println(FILENAME);
}
}

Output:

Application-2.0.2-TEST-1.0.0.zip

ONLINE DEMO

Regular Expression to find a string included between two characters while EXCLUDING the delimiters

Easy done:

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

Technically that's using lookaheads and lookbehinds. See Lookahead and Lookbehind Zero-Width Assertions. The pattern consists of:

  • is preceded by a [ that is not captured (lookbehind);
  • a non-greedy captured group. It's non-greedy to stop at the first ]; and
  • is followed by a ] that is not captured (lookahead).

Alternatively you can just capture what's between the square brackets:

\[(.*?)\]

and return the first captured group instead of the entire match.

Regular expression to remove substring with known start and end pattern, with multiple words in between

Use

/\bOn\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept?|Oct|Nov|Dec)\s+(?:0?[1-9]|[12]\d|3[01]),\s*\d{4}\s*(?:0?[0-9]|1[0-2]):[0-5]?[0-9]\s*[AP]M,/gi

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
On 'On'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
Jan 'Jan'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Feb 'Feb'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Mar 'Mar'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Apr 'Apr'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
May 'May'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jun 'Jun'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Jul 'Jul'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Aug 'Aug'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Sep 'Sep'
--------------------------------------------------------------------------------
t? 't' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Oct 'Oct'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Nov 'Nov'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
Dec 'Dec'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[12] any character of: '1', '2'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
3 '3'
--------------------------------------------------------------------------------
[01] any character of: '0', '1'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
0? '0' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
1 '1'
--------------------------------------------------------------------------------
[0-2] any character of: '0' to '2'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
[0-5]? any character of: '0' to '5' (optional
(matching the most amount possible))
--------------------------------------------------------------------------------
[0-9] any character of: '0' to '9'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[AP] any character of: 'A', 'P'
--------------------------------------------------------------------------------
M, 'M,'

Or, simpler one if the strings you deal with are in good shape:

/\bOn\s+\w+\s+\d{1,2},\s*\d{4}\s*\d{1,2}:\d{1,2}\s*[AP]M,/gi

See this proof.

EXPLANATION

--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
On 'On'
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[AP] any character of: 'A', 'P'
--------------------------------------------------------------------------------
M, 'M,'


Related Topics



Leave a reply



Submit