Jump to content

Truncating string signals


PabloR

Recommended Posts

I would like to truncate string signals to a given number of characters.

As an example, say the signal gives a combination of an #ID_date_Initials (e.g. 12345_20200326_PJR).

How can I generate a new signal that contains only part of the string? 

In the example above, I would like to get only the #ID.

Link to comment
Share on other sites

Hi Pablo,

you can use transform() and replace() to do this. I made an example with a signal that contains the following data:
image.thumb.png.d5ec3642f3d1b1e3b9ae585685f5e0c3.png

To create a signal that contains only the numeric values I used the following formula:

$originalSignal.transform(($p, $c) -> sample($c.getKey(), $c.getValue().replace('/\\w+\\s(\\d+)/', '$1')))

Transform is used to access every sample in the signal by specifying a lambda expression. The current sample is temporarily stored in the variable $c. $p contains the previous sample which is not used here. For each sample of the original signal a new sample is created by using the timestamp (key) of the original sample. The value for the new sample is determined by the value of the sample of the original signal on which replace() is executed. As the name suggests replace() is used to replace portions of a string. In this case I make use of a regular expression to determine the numeric value inside the original string and replace the original string by the determined value. The result looks like this:

image.thumb.png.5f5b80238bb4aafcdccb848c5bb0726e.png

For your example (12345_20200326_PJR) you have to modify the replace part to:

.replace('/(\\d+)_\\d+_\\w+/', '$1')

Hope this helps.

Regards,

Thorsten

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

  • Administrators

The replace operator can be applied a signal without a transform.  Using the replace function without the transform will be less computationally expensive, resulting in better performance. Both ways will get to the same answer.

Syntax for replace function without transform:

$signal.replace('/(\\d+)_\\d+_\\w+/', '$1')

 

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...