Questions tagged [regex-lookarounds]

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match at the current match position.

Regular expression lookarounds are zero-width assertions that verify conditions about the context of a match. Lookarounds will actually match characters, but then give up the match and only return the result: match or no match. That is why they are called "assertions". They do not consume characters in the string, but only assert whether a match is possible or not.

3265 questions
9
votes
2 answers

JS Regex lookbehind not working in firefox and safari

I have this following regex which is working in chrome but causes an error in firefox or safari. I need to modify it to make it work. Can anybody help out a poor soul? Thanks in advance! regex: /(?=)(.*?)(?<=<\/tag>)/ Basically, I have to match…
webedward
  • 93
  • 1
  • 1
  • 4
9
votes
4 answers

Regex get text before and after a hyphen

I have this string: "Common Waxbill - Estrilda astrild" How can I write 2 separate regexes for the words before and after the hyphen? The output I would want is: "Common Waxbill" and "Estrilda astrild"
Oliver Oliver
  • 2,057
  • 4
  • 16
  • 14
9
votes
2 answers

lookbehind for start of string or a character

The command re.compile(ur"(?<=,| |^)(?:next to|near|beside|opp).+?(?=,|$)", re.IGNORECASE) throws a sre_constants.error: look-behind requires fixed-width pattern error in my program but regex101 shows it to be fine. What I'm trying to do here is to…
anupamGak
  • 95
  • 1
  • 3
9
votes
2 answers

Can sed regex simulate lookbehind and lookahead?

I'm trying to write a sed script that will capture all "naked" URL's in a text file and replace them with [URL]. By "naked" I mean a URL that is not wrapped inside an anchor tag. My initial thought was that I should match URL's…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
8
votes
1 answer

Raku regex: How to use capturing group inside lookaheads

How can I use capturing groups inside lookahead assertion? This code: say "ab" ~~ m/(a) /; returns: 「a」 0 => 「a」 But I was expecting to also capture 'b'. Is there a way to do so? I don't want to leave 'b' outside of the lookahead…
Julio
  • 5,208
  • 1
  • 13
  • 42
8
votes
4 answers

RegEx for matching all chars except some special chars and ":)"

I'm trying to remove all characters from a string except for #, @, :), :(. Example: this is, a placeholder text. I wanna remove symbols like ! and ? but keep @ & # & :) should result in (after removing the matched results): this is a placeholder…
mahmoudafer
  • 1,139
  • 3
  • 14
  • 30
8
votes
1 answer

How to write a regex lookahead/lookbehind in mysql

I'm trying to do something like SELECT * FROM table WHERE column REGEXP (abc)(?=def) and I got the error Got error 'repetition-operator operand invalid' from regexp due to the '?' -> see #1139 - Got error 'repetition-operator operand invalid'…
guillaume latour
  • 352
  • 2
  • 18
8
votes
2 answers

Regex negative lookbehind and lookahead: equivalence and performance

I need a regex that will select only those URL strings NOT ending with specific extensions like .png or .css. I tested the following: 1) this one using negative lookbehind: (?
Timido
  • 1,646
  • 2
  • 13
  • 16
8
votes
2 answers

How to rewind next-search start position by 1?

How can I rewind the start of the next search position by 1? For example, suppose I want to match all digits between #. The following will give me only odd numbers. my $data="#1#2#3#4#"; while ( $data =~ /#(\d)#/g ) { print $1, "\n"; } But if I…
n.r.
  • 1,900
  • 15
  • 20
8
votes
2 answers

How to replace pattern of repeating characters/words only at the beginning of the string?

Note that this question is in the context of Julia, and therefore (to my knowledge) PCRE. Suppose that you had a string like this: "sssppaaasspaapppssss" and you wanted to match, individually, the repeating characters at the end of the string (in…
Glen O
  • 733
  • 3
  • 10
8
votes
2 answers

Syntax for multiple positive lookaheads in JavaScript regex

I'm trying to include two positive lookaheads in one regex. Here's the problem I'm working on as an example. (?=[a-zA-Z])(?=[0-9])[a-zA-Z0-9]{0,20} This is what I'm trying to match: 0-20 characters one or more letter anywhere one or more number…
Qaz
  • 1,556
  • 2
  • 20
  • 34
8
votes
8 answers

Look behinds: all the rage in regex?

Many regex questions lately have some kind of look-around element in the query that appears to me is not necessary to the success of the match. Is there some teaching resource that is promoting them? I am trying to figure out what kinds of cases you…
beroe
  • 11,784
  • 5
  • 34
  • 79
8
votes
3 answers

JavaScript support of Lookaheads and Lookbehinds in Regular Expressions

Does JavaScript support positive and/or negative lookaheads/lookbehinds? Which combinations of them? Or, to be more specific: Positive lookaheads Negative lookaheads Positive lookbehinds Negative lookbehinds
Mario Rossi
  • 7,651
  • 27
  • 37
8
votes
3 answers

Match a pattern only once

I have a string foo-bar-bat.bla I wish to match only foo My flawed pattern matches both foo and bar \w+(?=-.*\.bla) How do I discard bar? Or maybe even better, how could I stop matching stuff after foo?
wawiwa
  • 313
  • 1
  • 5
  • 9
8
votes
3 answers

Match string with Regex as long as it is not surrounded by parentheses

I am looking to match a string "Order By XXX" where XXX can be any letter, number, period, comma, space or square bracket. However, I would only like to match this if it is not surrounded by parentheses (parentheses on one side is ok, as long as it…
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174