Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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.

Product Version: UE 4.22
more ▼

asked May 02 '19 at 07:20 AM in C++ Programming

avatar image

25 2 2 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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:



more ▼

answered May 02 '19 at 09:02 AM

avatar image

1.5k 6 6 12

avatar image Bazzy2000 May 02 '19 at 09:40 AM

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.

avatar image Bazzy2000 May 02 '19 at 09:50 AM

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(" ");


avatar image phil_me_up May 02 '19 at 10:32 AM

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++)
          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)

avatar image Bazzy2000 May 02 '19 at 10:52 AM

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()).

avatar image phil_me_up May 02 '19 at 10:59 AM

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.

avatar image Bazzy2000 May 02 '19 at 11:11 AM

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

avatar image Bazzy2000 May 02 '19 at 11:13 AM

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.

avatar image phil_me_up May 02 '19 at 11:23 AM

The answer to this question provides an example: https://answers.unrealengine.com/questions/419704/regex-in-unrealengine.html 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/

avatar image Bazzy2000 May 02 '19 at 11:42 AM

Okay, thank you!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question