0

Here is an example string. I have many more of these that I need to fix:

"måde", "answer" => "råd");

In this string there are 2 instances of the character å. I want find a match for the å in råd using regex, while ignoring the the å in måde.

So basically I need to find a match of an occurrence of å after answer.

I thought that (?<=answer)å(?=\;) would work, but I'm really new to this and would very much appreciate it if someone could point me in the right direction.

I tried asking this question before but got downvoted for some reason, so I tried to reformulate it here. I hope it is clear enough.

Moogal
  • 107
  • 1
  • 9

2 Answers2

3

Look behinds (typically) can't be of variable length, but by using a negative look ahead, you tell it what not to match:

 å(?!.*answer)

This matches "å", but only if "answer" doesn't appear somewhere after it.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

(?<=answer.*?)å(?=.*?;) should work (I don't think you need to escape the semicolon, but depending on your application you might need to reescape it).

Essentially: Lookaheads/Lookbehinds still "match" at the position you're at.

edit: Many regex engines (including Sublime Text 2's) don't like quantifiers in lookahead/lookbehind assertions. Use (?<=answer).*?å.*?(?=;) instead and search in the resulting match with a second regex.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
  • 1
    This should work in .NET. However not all languages support quantifiers in look-ahead/look-behinds. If you can not use * in your look-ahead, just get the substring behind "answer" with (?<=answer).*(?=;) and find all matches of å in that substring with a second regular expression :) – H W Feb 02 '15 at 13:46
  • Just tested it, and indeed Sublime Text 2 has a problem with it. Good point. – L3viathan Feb 02 '15 at 13:48
  • Thanks very much. I've had a similar result with `(?<=answer).*(?=;)`, but don't know how to search with a second regex – Moogal Feb 02 '15 at 13:58
  • That will be difficult to do in a text editor, but easy in most scripting languages. – L3viathan Feb 02 '15 at 14:00