Regex of +|- return null

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By muncuss

Hello, i am trying to find either + or - in a string like “apple + 6” with regex.compile(“+|-”) but return null. maybe it needs escape character but im trying with \+|- it said invalid escape sequence. tried to double it (“\\+|-”) only worked once and now it returns also null

:bust_in_silhouette: Reply From: jgodfrey

Both of those characters need to be escaped. So, something like this should work:

var regex = RegEx.new()
var string = "apple + 6"
regex.compile("\\+|\\-")
var result = regex.search(string)
if result:
	print(result.get_string())

thanks for the answer but i just realized i put the wrong string variable lol

muncuss | 2022-02-03 12:19