How to Match Something With Regex That Is Not Between Two Special Characters

How to match something with regex that is not between two special characters?

Assuming the quotes are correctly balanced and there are no escaped quotes, then it's easy:

result = subject.gsub(/a(?=(?:[^"]*"[^"]*")*[^"]*\Z)/, '')

This replaces all the as with the empty string if and only if there is an even number of quotes ahead of the matched a.

Explanation:

a        # Match a
(?= # only if it's followed by...
(?: # ...the following:
[^"]*" # any number of non-quotes, followed by one quote
[^"]*" # the same again, ensuring an even number
)* # any number of times (0, 2, 4 etc. quotes)
[^"]* # followed by only non-quotes until
\Z # the end of the string.
) # End of lookahead assertion

If you can have escaped quotes within quotes (a "length: 2\""), it's still possible but will be more complicated:

result = subject.gsub(/a(?=(?:(?:\\.|[^"\\])*"(?:\\.|[^"\\])*")*(?:\\.|[^"\\])*\Z)/, '')

This is in essence the same regex as above, only substituting (?:\\.|[^"\\]) for [^"]:

(?:     # Match either...
\\. # an escaped character
| # or
[^"\\] # any character except backslash or quote
) # End of alternation

Regex to match certain characters anywhere between two characters

You can match substrings between square brackets and then remove all chars that are not punctuation:

const text = '[abc.] [!bc]. [.e.g]';
const matches = text.match(/\[([^\][]*)]/g).map(x => `[${x.replace(/[^.,?!]/g, '')}]`)
console.log(matches);

Regular Expressions - Match Character Not Between Two Strings

This does the job even with nested tag:

  • Find: \[(\w+)\].+?\[/\1\](*SKIP)(*FAIL)|&
  • Replace: @

Demo & explanation

How it works:

  • \[(\w+)\].+?\[/\1\] is trying to match opening and closing tag with some data inside
  • (*SKIP)(*FAIL) if tag is found, then discard it
  • | else
  • & match an ampersand. At this point, we are sure it is not inside a tag.

Unfortunately this doesn't work with Java, but this requirement was only added after I answered.

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.

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

Regex Match all characters between two strings

For example

(?<=This is)(.*)(?=sentence)

Regexr

I used lookbehind (?<=) and look ahead (?=) so that "This is" and "sentence" is not included in the match, but this is up to your use case, you can also simply write This is(.*)sentence.

The important thing here is that you activate the "dotall" mode of your regex engine, so that the . is matching the newline. But how you do this depends on your regex engine.

The next thing is if you use .* or .*?. The first one is greedy and will match till the last "sentence" in your string, the second one is lazy and will match till the next "sentence" in your string.

Update

Regexr

This is(?s)(.*)sentence

Where the (?s) turns on the dotall modifier, making the . matching the newline characters.

Update 2:

(?<=is \()(.*?)(?=\s*\))

is matching your example "This is (a simple) sentence". See here on Regexr

Use regex to find text NOT between two characters

Assuming your curly braces will always be balanced, you can use a Negative Lookahead here.

[a-zA-Z]+(?![^{]*\})

Note: This only matches characters of the A to Z range, if words will include whitespace or you have other characters that need to be allowed or unsure of the type of input, you can use a negated match here.

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

Live demo

Regex: match all special characters but not *

What you really want is this regex for matching:

[^\w\s*]+

Replace it by empty string.

Which means match 1 or more of any character that is:

  1. Not a word character [AND]
  2. Not a whitespace [AND]
  3. Not a literal *

RegEx Demo

Regex Match special characters between two characters

You may use

^[a-zA-Z]+(?:[-.'][a-zA-Z]+)*$

See the regex demo

Details

  • ^ - start of string
  • [a-zA-Z]+ - 1+ ASCII letters
  • (?:[-.'][a-zA-Z]+)* - 0 or more occurrences of
    • [-.'] - a hyphen, dot or single quote
    • [a-zA-Z]+ - 1+ ASCII letters
  • $ - end of string

how to match a text including special characters in using regex in oracle

You can use

Process\s+explanation\s*:\s*(.*?)(\.\s*Final activity|$)

See this regex demo. Details:

  • Process - a word
  • \s+ - one or more whitespaces
  • explanation - another word
  • \s*:\s* - a colon enclosed with zero or more whitespaces
  • (.*?) - Group 1: any zero or more chars as few as possible
  • (\.\s*Final activity|$) - Group 2: either ., zero or more whitespaces, and Final activity or end of string.

In Oracle, you can use

select regexp_substr('Process explanation: plasma gasification combined with centrifugal activity. Final activity for manager: some activity',
'process\s+explanation\s*:\s*(.*?)(\.\s*Final activity|$)',
1, 1, 'i', 1) as extracted from dual;

The i option makes the pattern case insensitive and the last 1 argument tells Oracle to get the first capturing group value as output.

See a demo screenshot:

Sample Image

Sample Image



Related Topics



Leave a reply



Submit