LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Search and Replace String - How do you reference a Capture Group in your replace string value?

Solved!
Go to solution

I am using LabVIEW 2019 on Windows 10 and Windows 11 systems.
I want to use a regular expression to find & replace trailing 0's in a csv file.
I want to do this for my tab (`\t`) delimitered, CRLF EOL'd (`\r\n`) file.
I want to be able to have my regex accomplish this in one pass, but I can't figure out the syntax to reference a capture group.
I can accomplish this in 2 passes (top example) but this is inefficient for a ~20k line file.
My concern is that LabVIEW (or at least my version) does not support this feature.
block_diagram.pngfront_panel.png


I am able to do this with python:


# regex_poc.py
# regular expression proof of concept python file
import re


def main():
    source = '''
    Test 1.030  2.000   3.00000 4.70
    Test 5.000  6.000   7.00000 8.00
    Test 9.070  10.000  11.00000    12.70
    '''
    p = r"(?:(?:\.)?0+)([\t|\n])"
    soln = re.sub(pattern=p, repl=r"\1", string=source)
# the `repl=r"\1"` means replace value is first capture group of the pattern, p
# this is what I wish to replicate in LabVIEW's Search and Replace String function
    print(soln)


if __name__ == "__main__":
    main()
    """
    CMD execution & output, on win11, python 3.13:
    C:\temp>python regex_poc.py

    Test 1.03   2       3       4.7
    Test 5      6       7       8
    Test 9.07   10      11      12.7
    """


....but I am not able to figure out how to do it in LabVIEW -- which is where the codebase I need to implement this in actually lives.
Does anyone know how to do this?
I have search the NI forums, and searched the internet more broadly for help with this, but no luck yet.
Please let me know if I can/should provide more details.
And thank you in advance.

0 Kudos
Message 1 of 3
(537 Views)
Solution
Accepted by topic author TK421_AWOL

This is from the help file for "Special Characters for Match Regular Expression and Search and Replace String":

 

Backreferences in Replace String

When you use the Search and Replace String function in Regular Expression mode, you can specify a backreference in the replace string input that refers to submatches in the search string input. Use $1 to refer to the first submatch, $2 to refer to the second submatch, and so on. You can use these special characters only in the replace string input of the Search and Replace String function. Use these special characters to insert a string you specify before or after the submatch you specify. Consider the following example:

input string: $value "TRUE"TRUE" *NULL
search string: (["*$])(\w+)\1\2\1
replace string: $1$2value$1

result string: $value "TRUEvalue" *NULL

In this example, the $1 backreferences in replace string refer to the first submatch in search string. Likewise, the $2 backreference refers to the second submatch in search string.

 

 

I think this is what you are asking about, but I am not 100% sure and since you didn't post your example code as a VI I can't try it without replicating it myself, which I am not going to do....

Message 2 of 3
(520 Views)

This did exactly what I needed, thank you!

 

(I just re-checkedd my help file for Search and Replace String Function, but I do not find a link to this secondary help file.)

0 Kudos
Message 3 of 3
(456 Views)