Skip to main content
Contributor
Aug 16, 2024 9:36 PM UTC
Solved

Parser Business Rule

I am trying to write a parser business rule to give the portion of a string that begins with US and the 6 characters after from a data source.

For example, the imported line reads "mmmmmmmmmUSxxxxxxmmmmmmm" and I want to parse out USxxxxxx to which is mapped in the transformation rules.  I also need it to be dynamic because the line will not always be in the same character place.  It could also be "mmmmmUSxxxxxxmmm" or anywhere else within the text.  I have tried using the rule below but it is not bringing back any values.  The import is reflecting "blank."

 

Dim sText As String = args.Value

Dim result As String = ""

If sText.Contains("US") Then

     result = sText.Substring(sText.IndexOf("US"),8)

End If

Return result

 

ChristianW
Employee - Advisor
Aug 19, 2024 8:32 AM UTC

You should have a look at regular expressions. It is a little cryptic but very powerful. Microsoft provides all the information you need to parse complex strings.

4 replies

Member
Aug 18, 2024 7:57 AM UTC

Hi,

The code is sound so issue must be that the If statement is evaluating to False. Add a debug statement to output the value of sText to the Error Log to see the string that is being evaluated.

Regards.

SH 

Expert
Aug 19, 2024 7:53 AM UTC

I would use XFContainsIgnoreCase rather than the standard VB Contains.

If sText.XFContainsIgnoreCase("US") Then

 

It won't make a difference to this script as the source is already upper case but it's a good practice to get into.

Secondly I agree with SimonHesford that a debug of some sort is needed as the code looks OK. I would have an Else statement such as:

If sText.XFContainsIgnoreCase("US") Then
    result = sText.Substring(sText.IndexOf("US"),8)
Else
    result = $"Text [{sText}] does not contain US"
End If

 

ChristianW
Employee - Advisor
Employee - Advisor
Aug 19, 2024 8:32 AM UTC

You should have a look at regular expressions. It is a little cryptic but very powerful. Microsoft provides all the information you need to parse complex strings.

Contributor
Aug 20, 2024 5:18 PM UTC

Thanks everyone for your help!  It is now working as expected.