1

I am looking for a way to create a user snippet to grab the core directory from a project. Here is the sample path that I am using:

C:dev/Pseudope/Unreal/Cubicle/Source/CubicleCore/Private

When creating vscode snippets ${TM_FILEPATH} will return this path to be used along side a regular expression. I want to be able to use these snippets for different projects that will all have the same structure as the filepath above where the text that I want to apply to the snippet will always be nested between Source and Private|Public. Here is the regex that I have so far:

(?<=Source\\)(.*)(?=\\Private|Public)

According to https://regexr.com/ this expression will target the value "CubicleCore" as well as group it into group 1. Unfortunately, there seems to be a disconnect between the regular expression in the browser and how vscode is interpreting it because the result I get is:

UE_LOG(Log${TM_FILEPATH/(?<=Source\)(.*)(?=\Private|Public)/}, Log, TEXT("Hello");

When it should be:

UE_LOG(LogCubicleCore, Log, TEXT("Hello");

Any help/direction would be greatly appreciated.

1 Answer 1

1

You can use

"UE_LOG(Log${TM_FILEPATH/.*Source[\\\\\\/]([^\\\\/]+)[\\\\\\/](?:Private|Public).*/$1/}, Log, TEXT(\"Hello\")"

See the regex demo.

Details:

  • .* - any zero or more chars as many as possible
  • Source - Source
  • [\\\/] - / or \
  • ([^\\\/]+) - Group 1 ($1): any one or more chars other than \ and /
  • [\\\/] - a / or \ char
  • (?:Private|Public) - Private or Public string
  • .* - any zero or more chars as many as possible.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the quick response. I appreciate the detailed explanation. Not only does this solve my question (I already tested in vscode) but it also provided a deeper understanding of regex thanks!
@Pseudope I should have mentioned that in the ${TM_FILEPATH/pattern/replacement/flags} you need to use the repalcement logic (you used a matching one), and you also need to double escape backslashes to form regex escapes, otherwise they are simply ignored if not part of valid string escape sequences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.