regexp "\n" in treplace

One Star

regexp "\n" in treplace

Hello,
I have to move from a string field all the "\n".
I have tried the EREPLACE and the CHANGE fonctions in tMap but the half run: the "\n" is reached but not replaced and the replace string is added after the "\n".
Then I have tried the tReplace, but I have the same problem.
One Star

Re: regexp "\n" in treplace

Hello
If you want to replace a character you can use this function : yourtable.yourfield.replaceAll("\\\n", " ");
With that all "\n" will be replace by " "
the \\ is needed to make correctly this operation. I hope that will works
One Star

Re: regexp "\n" in treplace

Hello
If you want to replace a character you can use this function : yourtable.yourfield.replaceAll("\\\n", " ");
With that all "\n" will be replace by " "
I did that with Tmap but I don't know if it works with others components
the \\ is needed to make correctly this operation. I hope that will works
Employee

Re: regexp "\n" in treplace

In fact several syntaxes seems to work:
        String string = "test1\ntest2";
System.out.println("original:" + string);;
System.out.println("1:" + "test1\ntest2".replaceAll("\n", " "));;
System.out.println("2:" + "test1\ntest2".replaceAll("\\n", " "));;
System.out.println("3:" + "test1\ntest2".replaceAll("\\\n", " "));;
System.out.println("4:" + "test1\ntest2".replaceAll("\\\\n", " "));;

give result:
original:test1
test2
1:test1 test2
2:test1 test2
3:test1 test2
4:test1
test2
One Star

Re: regexp "\n" in treplace

I'm sorry but it doesn't work!
See datas please:
In the second image, you can see the input data. || is the \n.
In the third image, you can see the tMap function I use
In the first image the result.

You see that The job has found the \n but hasn't replace it. The job has added the space after the \n.
What is the problem in my job?
Thanks for reply.
Employee

Re: regexp "\n" in treplace

Ok, I found your problem, this a DOS file with \r\n (not only \n):
        String string = "test1\r\n\r\ntest2";
System.out.println("original:" + string);;
System.out.println("1:" + string.replaceAll("\n", " "));
System.out.println("2:" + string.replaceAll("\\n", " "));
System.out.println("3:" + string.replaceAll("\\\n", " "));
System.out.println("4:" + string.replaceAll("\\\\n", " "));
System.out.println("5:" + string.replaceAll("\r\n", " "));
System.out.println("6:" + string.replaceAll("(\r\n)+", " "));

give:
original:test1
test2
1:test1

test2
2:test1

test2
3:test1

test2
4:test1
test2
5:test1 test2
5:test1 test2

So the best pattern to use is:
(\r\n)+
One Star

Re: regexp "\n" in treplace

Thanks very much.
It's running well.
One Star

Re: regexp "\n" in treplace

Hi,
Ok, I found your problem, this a DOS file with \r\n (not only \n):
        String string = "test1\r\n\r\ntest2";
System.out.println("original:" + string);;
System.out.println("1:" + string.replaceAll("\n", " "));
System.out.println("2:" + string.replaceAll("\\n", " "));
System.out.println("3:" + string.replaceAll("\\\n", " "));
System.out.println("4:" + string.replaceAll("\\\\n", " "));
System.out.println("5:" + string.replaceAll("\r\n", " "));
System.out.println("6:" + string.replaceAll("(\r\n)+", " "));

give:
original:test1
test2
1:test1

test2
2:test1

test2
3:test1

test2
4:test1
test2
5:test1 test2
5:test1 test2

So the best pattern to use is:
(\r\n)+


Referring to no 5. How to remove space in between the test1 and test2? Instead of "test1 test2". I want it to be "test1test2". I have similar problem like this.
Thanks in advance!
One Star

Re: regexp "\n" in treplace

Hi,
use "" and not " " in replaceAll : string.replaceAll("(\r\n)+", ""))
best regards
Jeremie