#Regex "lookbehind assertion is not fixed length"

12 messages · Page 1 of 1 (latest)

whole prairie
#

Trying to match a letter at the end of a line only if there's a digit at the start, and an equals in the middle, and getting the mentionned error:

regex.compile("(?<=\\d.*= ?)[qwerty]\\b\\n?")```
I assume it's because i'm using the `*` and `?` characters which may match an unknown lengh and that godot can't handle that. Is there any way I can get around it, other than matching the whole line?
verbal jackal
#

(?:\d.*= ?)(?<my_letter>[qwerty])\b|\n|$?

#

regex.compile("(?:\\d.*= ?)(?<my_letter>[qwerty])\\b|\\n|$")

Then you can use regex_match.get_string("my_letter") to retrieve the captured letter

#

I'm not sure why you were using a negative lookahead, it doesn't seem necessary, but maybe I'm misunderstanding your requirements. If you post some examples of strings that should match, and examples of strings that should not match, maybe I can help more

whole prairie
#

idrk what im doing ive never really used regex before 😅
im using a modified ConfigFile thus need to replace letters with the values from an enum before godot will parse correctly

[sequence] ; only inside section 'sequence'

; should match
1.1.0 = e
5.2.3 = Q
8.4.52=q

; should not match
words = r
4.2.7 = w and something after
45.7.3 = and_some_other_string

; shouldnt match but can match for simplicity
123something = y
#

and these:

1.1.0 = e
5.2.3 = Q
8.4.52=q

will get converted to

1.1.0 = 2
5.2.3 = 0
8.4.52 = 0
verbal jackal
#

This should work:
(?i)(?:(?:\d+\.)+\d+\s?=\s?)(?<my_letter>[qwerty])\n|$

(?i): case insensitive
(?: do not capture

\d+ one or more numbers
\. a dot
So
(?:\d+\.)+ means "a number or more then a dot, at least once"
And then this:
(?:\d+\.)+\d+ means "a number or more then a dot, at least once, and at the end a number"
\s a space
followed by any of qwerty, and then either an end of line, or end of file

#

Instead of initial (?i), you can also use [qQwWeErRtTyY]

whole prairie
#

tysm it works!

#

regex is super confusing and i dont know it well lol