Skip to main content
Question

Extracting multiple substrings from a text string

  • February 18, 2025
  • 0 replies
  • 277 views

Forum|alt.badge.img+1

I have a text string that contains data similar to the following:

Company Name 1  |  P25021201 - 4341297 |  stp01YWTFMSXKS5f6u2z |  $1200.00 | Company Name 2  |  P25021201 - 4341112 |  stp01RFEQRMDTC5f6u0l |  $2400.00 | Wallet

I need to extract the data that starts with "stp" and return it in the following format: stpxxxx1, stpxxxxx (i.e. stp01YWTFMSXKS5f6u2z, stp01RFEQRMDTC5f6u0l

The problem is the number of "stp" strings in the text is unknown.  Does QB support a looping mechanism?  If yes, I could simply find the 1st stp string, remove it from the text string and keep looping until no more stp strings are found.

Any help with this is greatly appreciated as it is causing an issue with a production app.



MarkShnierYou
Forum|alt.badge.img+24

It is probably possible to create multiple child records, using Regex syntax in a pipeline, but that might be a little too fancy for both of us.

 

Assuming that there is some practical limit on the number of step strings like say 20 or even 50, it would be possible to use either a series of separate fields or one giant field with formula variables, and it would use the FIND function to locate the first occurrence coming out the string from the left. Then you could extract off the first one and then use the NotLeft function to hive off the string up to the end of the first stp string, then repeat the process like 20 times.

 

Each of the extracted strings would be in a separate form of variable, and then you would use the list function to list them all together, separated by a comma space 

 

 


MarkShnierYou
Forum|alt.badge.img+24

I think you can jusy repeat this like 20 times.  This one does the first two.  You can just repeating the pattern.

 

var text StringOneStart = NotLeft([Stp String], Find([Stp String],"stp")-1);
var text StringOne = Trim(Left($StringOneStart, "|"));

var text StringTwoStart = NotLeft($StringOneStart, Find([Stp String],"stp")-1);
var text StringTwo = Trim(Left($StringOneStart, "|"));


List(", ", $StringOne, $StringTwo)


Forum|alt.badge.img+1
  • Author
  • Registered
  • February 19, 2025

The issue with that approach is the number of "stp" strings is unknown.  If QB formulas supported looping it would be easy to solve.


Mez
Forum|alt.badge.img+7
  • Registered
  • February 19, 2025

If you want to venture into pipelines, you can achieve this with a simple regex and loop. 

If you have existing data you want to apply this to, start your pipeline with a 'search' step, then inside the loop just use an 'update record' step. And, once you update existing records and know that no other record has come during or after, then swap the search for on new event so you can update the records in near real-time. Before swapping the steps move the 'update record' step outside the loop otherwise it will be deleted when you delete the loop. 

Assuming you have a plan text field in your table to put the results to, you'll switch the entry of the filed to code using '</>' and input the below: 

*assumes your current field with this data is 'comments', we'll input our returned data into 'transformed comments'. Fields to update would be 'transformed comments' and then:

{% set match = a.comments | regex('(stp\w+)', true) %} 
{% if match is iterable %} 
{% for i in match -%}
{{ i }}
{%- if loop.last == false %}, {% endif -%}
{%- endfor %}
{% else %} 
No match
{% endif %}

** you may have to test for only a single 'stp...' value as we are iterating over the matched array.