Godot Version
4.3
Question
I’m making a text based game and one of the main ways of interacting with it is through clicking metas in RichTextLabels. In order not to go insane I decided to define a Dictionary with the words I need to be ed.
The idea is that the word I need is just the word that’s written down in the dictionary. If the word is followed or preceded by other letters then it isn’t ed. BUT if it is followed of preceded by punctuation, it does get ed.
The way I went about it was the following:
var word_dictionary : Dictionary {"word1" : "word kind", "word2" :"word kind"....}
func check_page_array(page_array: Array[String]) -> Array[String]:
var processed_pages : Array [String]
var regex_array : Array[RegEx]
for word in dictionary:
var regex = RegEx.new()
#regex.compile("\\b(?i)%s\\b" % word)
regex_array.append(regex)
var regex_patterns : Array
for regex in regex_array:
regex_patterns.append(regex.get_pattern())
for child in page_array:
var page : String = child
for pattern in regex_array:
match pattern.search_all(child):
[]:
pass
_:
var matches_array : Array[RegExMatch] = pattern.search_all(child)
var words_array : Array[String]
for reg_match in matches_array:
words_array.append(reg_match.get_string())
for word in words_array:
var clickable_word: String = "[url]%s[/url]" % word
page= page.replace(word, clickable)
processed_pages.append(page)
return processed_pages
For the most part, this worked. But with some new text I’ve been importing it suddenly started matching with the plural of some of the words. I don’t understand why and can’t seem to find a way to solve it. I tried using Regex101 to try ideas but the pattern seems to be alright and I even asked ChatGPT a hundred times in different ways to no avail.
I really need some help here.
I apologise if I’m not clear enough, english isn’t my first language and I started coding like a year agon on my own. I still have lots to learn.
Many thanks