Filtering out URL by string

I’m doing a HTTP request in C++ project and I have this link that has many urls in it. My HTTP request is working and it prints the Json array to the console log, but I want to filter out those urls by string. For example I want to print only specific urls that for example has “TEXT” word in them. Any help would be appreciated.

I’m not 100% what you want to do, so I’ll assume you want to selectivly choose what you print out to the console log. In that case, just wrap your log statements in an if which either uses something like FString::Contains(TEXT(“MyText”)) or for more complex cases, use RegEx:

https://api.unrealengine.com/INT/API/Runtime/Core/Internationalization/FRegexMatcher/__ctor/index.html

I want to filter out urls by string with a specific key word in them. For example if there is a Json array that contains these urls:
URLs:
[
X.url
,X.url
,W.url
]

I want to select and print only urls that has X word in them.

This is my HTTP request code that has a for loop in it that currently prints the whole array to the Output Log.

/Assigned function on successfull http call/
void FXPluginModule::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
//Create a pointer to hold the json serialized data
TSharedPtr JsonObject;

//Create a reader pointer to read the json data
TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

//Deserialize the json data given Reader and the actual object to deserialize
if (FJsonSerializer::Deserialize(Reader, JsonObject))
{
	//Get the value of the json object by field name
	TArray<TSharedPtr<FJsonValue>> objArray = JsonObject->GetArrayField("Urls");
	UE_LOG(LogTemp, Warning, TEXT("HTTP Request Successful"));
	UE_LOG(LogTemp, Warning, TEXT("Printing URLs......"));
	GLog->Log(" ");
	
	// Prints urls to the Output Log
	for (int32 index = 0; index < objArray.Num(); index++)
	{
		GLog->Log("URL: " + objArray[index]->AsString());
		GLog->Log(" ");
	}
}

}

Either FString::Contains or regex would do that for you.

In its simplest form you could have:

for (int32 index = 0; index < objArray.Num(); index++)
     {
for(int32 filterStr = 0; filterStr < FilterStrings.Num(); filterStr++)
{
if(objArray[index]->AsString().Contains(FilterStrings[filterStr]))
{
         GLog->Log("URL: " + objArray[index]->AsString());
         GLog->Log(" ");
     }
}
}

That’s not very nice or efficient, but you get the idea. Take that as a starting point and when you’re happy it’s working, maybe take a look at using RegEx instead (more complex, but much more flexible)

Can you explain to me how does this algorithm really work and how to use it? I don’t understand this part (filterStr < FilterStrings.Num()).

That’s the same as the for loop above it (where you are using index and objArray). In your case the for loop will initially set the variable index to 0 and go through the code contained within the loop. Once done you’ll increment the value of index by 1 (index++) and if that value is less than the number of items in objArray, the loop will run again. Repeat until index >= objArray.Num().

In the example I provided it’s exactly the same, but I called the variable filterStr (you could easily call it ‘i’, ‘j’, ‘x’, ‘myVar’ or whatever you want as long as it’s not ‘index’ or something else which is already used). The FilterString is an array which you’d need to create which should contain all the strings you want to filter by. You’ll need to define FilterString either higher within the method (outside the for loops) or within your .h file.

The end result is that for everything in objArray, we’ll check against every entry in FilterString and if we find that the string from the objArray contain a string in FilterString, you’ll pass the if condition and then print the log.

Like I said, it’s not the best way to do it, but it is a simple and understandable way.

Thank you very much for the help and explanation. I got it to work!

Is there a tutorial for using regex inside UE4? I searhced online and found only one question about it that didn’t really relate to my situation.

The answer to this question provides an example: RegEx in UnrealEngine - Programming & Scripting - Unreal Engine Forums and looks similar to how I use it.

However, the first starting point is to become familiar with RegEx as it’s not quite as straight forward as you might think. This is a good tool: https://regexone.com/ as is this: https://regexr.com/

Okay, thank you!