PabloR Posted March 26, 2020 Share Posted March 26, 2020 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 More sharing options...
Thorsten Vogt Posted March 27, 2020 Share Posted March 27, 2020 Hi Pablo, you can use transform() and replace() to do this. I made an example with a signal that contains the following data: 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: For your example (12345_20200326_PJR) you have to modify the replace part to: .replace('/(\\d+)_\\d+_\\w+/', '$1') Hope this helps. Regards, Thorsten 3 1 Link to comment Share on other sites More sharing options...
Administrators Teddy Posted March 27, 2020 Administrators Share Posted March 27, 2020 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') 2 2 Link to comment Share on other sites More sharing options...
Seeq Team Morgan Bowling Posted April 9, 2020 Seeq Team Share Posted April 9, 2020 Elaborating on this, If you just want to grab the first character of your string you can do $siganl.replace('/(.).*/','$1') and the second character would be $siganl.replace('/.(.).*/','$1') and so on. Morgan 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now