-1

I have below object returned by a particular function

    {"count":3,
      "items":[
       {
        "organizationCode": "FP1",
        "organizationName": "FTE Process Org"
      },
      {
        "organizationCode": "T11",
        "organizationName": "FTE Discrete Org"
      },
      {
        "organizationCode": "M1",
        "organizationName": "Seattle Manufacturing"
      } ]
};

user has to search whether particular string exist in this object or not. If user need to search T11 then he can enter either T11 or 'T11' or "T11" . all this case should be accepted. If user enteres T1 then it should not be accepted(I am mentioning because T11 contains T1). if user enters "Seattle Manufacturing" it should be accepted I am trying to use contain method but its not working. How can I do that?

I am struggling because there is very little material available regarding freemarker

Shruti sharma
  • 199
  • 6
  • 21
  • 67
  • 1
    You need to be more specific. Does the search need to be a complete match? case sensitive? etc... – emsimpson92 May 26 '20 at 23:06
  • it should be case insensitive. yes it should be complete match. "Seattle Manufacturing" should return true but if there is spelling error it should be false. fp1 or FP1 should return true but only fp or FP should return false – Shruti sharma May 26 '20 at 23:35
  • 1
    Are you sure handling this should belong to FreeMarker? It doesn't sound like that. What gives that result back? Is it a Java call on the back-end? Then maybe you should process the user input and do the filtering there, and the template just shows the result. – ddekany May 27 '20 at 08:10
  • We are using freemarker in otacle digital assistant(chatbot). Indeed it was a java call and above object was returned from restAPI. After result came, we have to compare in chatbot with user input. in chatbot we are using apche freemarker. is the above scenario possible in apache free marker? – Shruti sharma May 27 '20 at 08:27
  • @ddekany Could you please help me on the same, Please? – Shruti sharma May 27 '20 at 16:43
  • What variables are exposed to the template, and what type of values are they. Like, that JSON you show, let's call it `choices`, do you get it as a single string (which is bad news), or you can access parts of it in FreeMarker like `choices.items[0].organizationCode`? And again, can't the REST service be more functional and not push this on the template? Again, it's quite atypical for a template to do things like this. – ddekany May 28 '20 at 18:06
  • Actually rest is already designed. We need to customize output based on our need. I am getting output as json only. – Shruti sharma May 30 '20 at 21:48

1 Answers1

1

So the first step would be to accept the user input. Then if they include ' or " in their query, strip those characters off of their search. It's not necessary for this pattern. Once you've done that, you can use this pattern to find matches (?<=organization(?:Name|Code)":)\s+(?i)"T11". You'll want to replace the T11 portion of the pattern with whatever their stripped query is. This will not be case sensitive. If you need the pattern to be case sensitive you can remove the (?i).

Demo

emsimpson92
  • 1,779
  • 1
  • 9
  • 24
  • its not working for 'T11' or "T11" .double quote and single quote are part of user input. – Shruti sharma May 26 '20 at 23:36
  • 1
    Your answer implies that OP has to searching in a flat text JSON dump of the data. That would be an awful situation. This is structured data there. I'm hoping OP doesn't get it as a single big `String`. If he/she does, then it has to be parsed first (looks like JSON to me). – ddekany May 27 '20 at 08:18
  • 1
    @ShrutiSharma as I mentioned in the very first part of my answer, if they use single or double quotes in their query, remove them from the query string. – emsimpson92 May 27 '20 at 18:04
  • 1
    @ddekany you're correct, but if it's not a JSON string then the solution isn't a regex solution anyway. They asked a regex tagged question so I made assumptions based on that. – emsimpson92 May 27 '20 at 18:05
  • @ddekany yes it is JSON returened by API. I have to search what user entered is exist in API output or not. is it possible in freemarker? – Shruti sharma May 27 '20 at 21:31