Skip to main content
Nov 18, 2013 8:47 AM UTC
Solved

In ACL language, how to remove line breaks in a characters string

In ACL language, how to remove line breaks in a characters string?

I try this command:

gsub("\r\n", "", $x);

But it doesn't work.

Can you help me?

    dgopoisAnswer
    Dec 2, 2013 12:23 PM UTC

    The PTC technical support has solved the problem, see below.

    The first parameter to the gsub() function is a regular expression string and that does not support C syntax like \r and \n for carriage return and line feed. Instead you would have to put that string in a variable and then use the variable as the parameter to the function.

    $s = "\r\n";
    gsub($s, ' ', $x);

    Or you could take if further with the following:
    $s = "one\r\ntwo"
    gsub("\r\n", "x", $s)

    If instead your string only has a \r or \n then the character class

    gsub("[\r\n]", "x", $s)

    would work. The first regular expression matches two characters whereas the second matches only one. The first result would have one 'x' but the second would have 'xx'.

    4 replies

    Nov 18, 2013 2:52 PM UTC

    I believe what you are looking for is chop($st,[int])

    This is like the Perl func and removes the number of chars specified by [int] from $st. The integer is optional, default is 1 character, which is very useful for dropping the newline chars.

    Nov 18, 2013 2:56 PM UTC

    Are you sure that you're getting a match? Set your gsub function to a variable and test the result. If the result is 0, then no matches were found in the string. Otherwise, it returns the number of matches that it found.

    Nov 18, 2013 3:26 PM UTC

    You might have better luck if you just search for "\n".

    dgopoisAuthorAnswer
    Dec 2, 2013 12:23 PM UTC

    The PTC technical support has solved the problem, see below.

    The first parameter to the gsub() function is a regular expression string and that does not support C syntax like \r and \n for carriage return and line feed. Instead you would have to put that string in a variable and then use the variable as the parameter to the function.

    $s = "\r\n";
    gsub($s, ' ', $x);

    Or you could take if further with the following:
    $s = "one\r\ntwo"
    gsub("\r\n", "x", $s)

    If instead your string only has a \r or \n then the character class

    gsub("[\r\n]", "x", $s)

    would work. The first regular expression matches two characters whereas the second matches only one. The first result would have one 'x' but the second would have 'xx'.