RegEx in UnrealEngine

Hello.

I am trying to use FRegexMatcher and can’t understand, why it doesn’t work.
My code is:

#include "Regex.h"
............................
// Matches:
//	"ntldll.dll"
const FRegexPattern myPattern(TEXT("^[a-z,A-Z,0-9,\\-,_]+\\.[exe|dll]"));

FRegexMatcher myMatcher(pattern, TEXT("ntldll.dll"));

if (myMatcher.FindNext()) {
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, TEXT("Found ! "));
}

But no messages shown.

What I am doing wrong?

I fixed variable name and pattern, now it works:

const FRegexPattern myPattern(TEXT("^[a-z,A-Z,0-9,\\-,_]+\\.[exe|dll]+"));
FRegexMatcher myMatcher(myPattern, TEXT("ntldll.dll"));

if (myMatcher.FindNext()) 
{
	int32 b = myMatcher.GetMatchBeginning();
	int32 e = myMatcher.GetMatchEnding();
	UE_LOG(X, Warning, TEXT("REGEX %i %i"), b, e);
}

Your rule will catch any combination of “e”, “x”, “d”, “l” letters.
Anyway you can use alternatives:

  • TEXT(“^[a-z,A-Z,0-9,\-,_]+\.dll”)
  • TEXT(“^[a-z,A-Z,0-9,\-,_]+\.exe”)

OR

  • TEXT(“^[a-z,A-Z,0-9,\-,_]+\.[d|e][l|x][l|e]”)
2 Likes

Thank you very much!

Please note that /[a-z,A-Z]/ will match all lower case and upper case letters as well as “,”. The comma is NOT a list item separator! You’re more likely looking for /[a-zA-Z0-9_-]/. The dash at the end of the list specifies it as a character, not a range indicator.

Additionally instead of /.[exe|dll]+/, which would also match “exdllex|ex|xelldl”, you’ll probably want /.(exe|dll)/ which matches exactly for the presence of either “.exe” or “.dll”.

Thanks you for posting this!

note: A potential problem with

TEXT(“^[a-z,A-Z,0-9,-,_]+.[d|e][l|x][l|e]”)

is that the [d|e][l|x][l|e] at the end will match any of the following 8 combinations of file extensions:
dll,dle,dxl,dxe,ell,ele,exl,exe

But you probably only want it to match:
dll,exe

What you could do is make the regular expression like this (just like Zyr said):
“^[a-z,A-Z,0-9,\-,_]+\.(exe|dll)+”

So now the extension has to be exactly exe, or exactly dll in order to match.

Basically, [] specify SETS of individual characters you can match, and () specify exact sequences of characters you can match.

example:

“[Foo]{2}”

  • you’re saying find 2 characters where each character is either f, or o
  • this can match ff, oo, of, fo
  • This regex is the same as just "[Fo]{2}

“(Barr){2}”

  • You’re saying look for the exact character sequence “Barr” twice
  • this can only match BarrBarr
  • Notice that unlike characters in [], repeated characters matter in ()

reference: