-2

I am trying to convert a certain regex from ECMAScript flavor to Golang flavor, here it is :

((r|)+)(( |\n)*)((|e)+)(( |\n)*)((p|)+)(( |\n)*)((|o)+)(( |\n)*)((|s)+)(( |\n)*)((t|)+)

Basically the point is to match messages like "r p O s t". I've tried to replace " " by "\s" but it's still not working. Any idea please?

  • Did the ECMAScript regex ever work properly? According to my calculations it would match `RR ee Pp OOOO s t` without issues. – MonkeyZeus Dec 24 '19 at 13:21
  • The `\s` will work for you. You just did not use the raw string literal. – Wiktor Stribiżew Dec 24 '19 at 13:22
  • Use `[r]+` rather than `(r|)+` - the latter is inefficient. The same applies to any alternations for single character matches: `[ \n]*`, `[e]+`, etc. – ctwheels Dec 24 '19 at 15:13

2 Answers2

1

Is this working for you?

[r]\s*[e]\s*[p]\s*[o]\s*[s]\s*[t]

With case insensitive flag

Demo & explanation

Toto
  • 89,455
  • 62
  • 89
  • 125
0

Your example doesn't match because your regex doesn't include an uppercase "O"

You can use this expression that includes it:

((r|)+)(( |\n)*)((|e)+)(( |\n)*)((p|)+)(( |\n)*)((|o|O)+)(( |\n)*)((|s)+)(( |\n)*)((t|)+)

In your Go code, make sure to use these quotes with it:

`((r|)+)(( |\n)*)((|e)+)(( |\n)*)((p|)+)(( |\n)*)((|o|O)+)(( |\n)*)((|s)+)(( |\n)*)((t|)+)`

Also see here

xarantolus
  • 1,961
  • 1
  • 11
  • 19