lookaround

regex: negative lookbehind in negation character class? (.NET flavour)

What I'm trying to do: remove innermost unescaped square brackets surrounding a specific, unescaped character (\ is escape) input: [\[x\]]\]\[[\[y\]] output when looking for brackets around y: [\[x\]]\]\[\[y\] output when looking for brackets around x: \[x\]\]\[[\[y\]] In short, remove only the unescaped set of brackets around the spec...

[Regexp] Stop matching when meeting a sequence of chars: fixing a lookbehind

Hello everyone! I have the following regexp: (?P<question>.+(?<!\[\[)) It is designed to match hello world! in the string hello world! [[A string typically used in programming examples]] Yet I just matches the whole string, and I can't figure out why. I've tried all flavors of lookaround, but it just won't work... Anyone knows how ...

Regex lookahead, lookbehind and atomic groups

Ive found these things in my regex buddy but i dont got a clue what for i can use them ? does somebody got some examples so i can try to understand how they work? (?!) - negative lookahead (?=) - positive lookahead (?<=) - positive lookbehind (?<!) - negative lookbehind (?>) - atomic group ...

Does lookaround affect which languages can be matched by regular expressions?

There are some features in modern regex engines which allow you to match languages that couldn't be matched without that feature. For example the following regex using back references matches the language of all strings that consist of a word that repeats itself: (.+)\1. This language is not regular and can't be matched by a regex, which...

How does the regular expression ‘(?<=#)[^#]+(?=#)’ work?

I have the following regex in a C# program, and have difficulties understanding it: (?<=#)[^#]+(?=#) I'll break it down to what I think I understood: (?<=#) a group, matching a hash. what's `?<=`? [^#]+ one or more non-hashes (used to achieve non-greediness) (?=#) another group, matching a hash. what's the `?=`? So the p...

extract last match from string in c#

i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match] i now want to match the string "our match" (i.e. without the brackets), so i played around with lookarounds and whatnot. i now get the correct match, but i don't think this is a clean solution. (?<=\.?\[) starts with '[' or '.[' ([^\[]*) ...

Regex optimization question

As a personal learning exercise, I wrote this regex to split a unary string into parts whose length is increasing powers of two (see also on ideone.com): for (String s : new String(new char[500]) .split("(?=(.*))(?<=(?<=(^.*))\\G.*)(?<=(?=\\2\\2.\\1)^.*)") ) { System.out.printf("%s ", s.length()); } ...

How can we match a^n b^n with Java regex?

This is the second part of a series of educational regex articles. It shows how lookaheads and nested references can be used to match the non-regular languge anbn. Nested references are first introduced in: How does this regex find triangular numbers? One of the archetypal non-regular languages is: L = { anbn: n > 0 } This i...

RegEx: Find word after a letter but don't include the letter in the result.

Here's a string that I may have: (MyStringIsOneWholeWord *) I have used the following javascript regular expression to get the text after the bracket if it starts with My. /(^|\s|\()+My(\w+)/g, The problem with this is that it includes the first bracket in the result, as that it is the letter/character that found it. How would I ...

How does this Java regex detect palindromes?

This is the third part in a series of educational regex articles. It follows How does this regex find triangular numbers? (where nested references is first introduced) and How can we match a^n b^n with Java regex? (where the lookahead "counting" mechanism is further elaborated upon). This part introduces a specific form of nested as...

How does this regex replacement reverse a string?

This is the fourth part in a series of educational regex articles. It show how the combination of nested reference (see: How does this regex find triangular numbers?) to "count" within assertions (see: How can we match a^n b^n with Java regex?) can be used to reverse a string. The programmatically generated pattern uses meta-pattern a...

Efficiency of lookarounds in C# regular expressions. Should I avoid them if I can?

Hello, everyone! I'm quite new to regular expressions, but I like them, A LOT! Call me nitpicky if you will, but I'd really like to know if I should avoid using lookaheads and lookbehinds if I have an option. For example, the two commands below do the same thing, one uses lookbehind and the other doesn't. the_str = Regex.Replace(the_s...