23
Views
11
Comments
How to get the word that is inside the delimiter
Question

When saving I need the word inside "{}", which in this case would be the word "idade", but using String_Split he is excluding the word inside "{}".  how to return only the word inside "{}" ?


Rank: #171

Hi Raquel,

Look the example in attachment.

I used the function String_LastIndexOf, to get the position of "{" and "}" in text, after, substring using positions.

Hope this helps.

Regards.

ExtractWordExample.oml

Hi Raquel,

Did you try using a regex expression (as /{(.*?)}/g ) inside of a js node?

Cheers,

António Pereira

mvp_badge
MVP
Rank: #2

Hi Raquel,

You shouldn't use String_Split, it's pretty useless especially if you have multiple words between {}. What I would advise is to use the built-in Function Index(), which will return the position of a Text inside a Text (so you could search for { and }), and then use Substr() to extract the string between the positions found. This is the easiest way. Of course you could use regular expressions, but that's usually a recipe for disaster if you don't know exactly what you're doing.

Rank: #112

Hello Raquel,

Hope you're doing well.

Another alternative could be the use of Index Built-In function alongside with SubStr Built-In function.

Something like this should do the trick:

Substr(Expression, Index(Expression, "{", 0) + 1, Index(Expression, "}", 0) - Index(Expression, "{", 0) - 1)


where Expression is the value for your attribute. In your case, it should be:

GetCampoTextoById.List.Current.CampoTexto.Mensagem


Hope that this helps you!


Kind regards,

Rui Barradas

Rank: #6177

@Rui Barradas @Agno Silveira 
this solution works and if the text has only one word with {}, how do I get a list of all words in the text with {} ?



Rank: #171

Hi Raquel,

More one .OML example in attachment, extract all words between {}.

Regards

:D


ExtractWordExample2.oml

Rank: #112

Hello again Raquel,

I will leave here just another example that may help you :)

The logic behind (creation of a list with the extracted words) is very similar to Agno's solution, but this one is fully based on SubStr() and Index() built-in functions.


Basically, what you have to do is:

1) Check if you still have delimiters in your text

2) If so, retrieve the word between the first 2 delimiters

Substr(TextToProcess, Index(TextToProcess, "{", 0) + 1, Index(TextToProcess, "}", 0) - Index(TextToProcess, "{", 0) - 1)

3) Append this word to a Text List variable (I'm doing this because I use this list after to show it on the screen)

4) Cut the processed part of the text and repeat the process for the rest until you have no more delimiters

Substr(TextToProcess, Index(TextToProcess, "}", 0) + 1, Length(TextToProcess))


Here's an example of this implementation:


You can test it directly in here: https://rbarradas.outsystemscloud.com/ExtractWords/Screen1


Please refer to attached OML :)


Kind regards,

Rui Barradas

ExtractWords.oml

Hi Raquel, 

You can use the server action "Regex_search" into text API and use the following pattern "(?<=\{)(.*?)(?=\})"


Here the sample. 

chaves.oml