function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vlrvlr 

How to filter out LF (break lines) and CR from values in the cells of CSV file

We have an email service which accepts CSV attachment and has process its values and insert it into the objects. CsvReader class stop processing lines from CSV when there is "Line Feed" present inside cell values.

User-added image

When I use this regex in Notepad++ to clean the the CSV file and then send it to the email service then the processing works fine and all lines from csv file are processed. The notepad++ regex is this:
 
(,"[a-zA-Z 0-9,.:']*)([\n\r]+)([a-zA-Z 0-9,.':]*")


Then I tried to use this regex in the apex code, in the Execute Autonomous Window, but I cannot get it match the text which I could match in Notepad++. And I cannot find out what kind of regex Apex is using. This what I tried so far:
 
String res ='1000000522,Lesly175,,Lastname523,10242016,,1234568411,,email_78@yahoo.com,123 Street Address,,West Hills,CA,California,US,United States,91304,,422 permanent street address,,West Hills,CA,California,US,United States,91304,Female,No,,,,U.S. Citizen,11/9/1992,United States,Yes,,,,,Yes,Spain (Castilian),,,,,,,,,,,,,,,,,,,,,,,,,Yes,,Not a member of the military,"My cumulative GPA at UCSB was a 2.98 and was not as high as it could have been, due to struggling with domestic abuse, a shoulder surgery, and neck surgery. I received over a 3.2 GPA during my last two years, and a 3.69 GPA during my last year, with a 3.89 my last quarter. This was after the surgeries and during the abuse. I continued to persevere despite those situations. They have made me a more disciplined and strong willed person, more determined than ever to succeed in USC\'s MSW program.",Yes,Yes,"I received a GPA lower than a 2.0 during my first quarter at UCSB in Fall of 2010. I was put on academic probation for Winter of 2010 and was required to pull my cumulative GPA to above a 2.0 by the end of the quarter. I met this requirement and was taken off probation. I was not able to perform to the best of my ability my first quarter, due to struggling with the end of severe depression and coming to terms with my sexuality. This showed me that I needed to keep moving forward and not give up.",,,,,,,,,,,,,,,,UNIVERSITY OF CALIFORNIA - SANTA BARBARA,SANTA BARBARA CITY COLLEGE,,,,,,,,,,,,,,reffirstname520,reffirstname523,,,,,,,,,,,,,,referencelastname523,referencelastname523,,,,,,,,,,,,,,Professor,Children\'s Service Administrator,,,,,,,,,,,,,,Yes,Yes,Yes,,,,,,,,,,,,,"Oct 19, 2016 at 8:00 PM","Oct 16, 2016 at 8:00 PM","Oct 23, 2016 at 8:00 PM",,,,,,,,,,,,,100000522,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,UCSB,15-Jun,No,,,Yes,"CYF Children, Youth, and Families",No,Yes,"University of California, Los Angeles\r\n'+
'California State University, Northridge",No,,No,Not Applicable,,,,,,,,,,,,,,,Graduate and Professional School Fair,Other,,,,,,,,,,,,,,"Graduate Fair at the University of California, Santa Barbara \r\n'+
'Santa Barbara, 2013",Social Work,,Social Work,,I am not eligible for military benefits,No,No,No,LGBTQ,Mental Health,Child and Family Services,2 Year,3 Year,Los Angeles,,No,No,No,No,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\r\n'+
'1000000523,Lesly176,Middlename2,Lastname524,10182016,,1234568412,,email.e79@gmail.com,,,Amherst,NY,New York,US,United States,14228,,423 permanent street address,,East Meadow,NY,New York,US,United States,11554,Female,No,,,,U.S. Citizen,7/12/1995,United States,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Not a member of the military,,No,No,,,,,,,,,,,,,,,,,UNIVERSITY AT BUFFALO,,,,,,,,,,,,,,,reffirstname521,reffirstname524,,,,,,,,,,,,,,referencelastname524,referencelastname524,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,100000523,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'+
'1000000524,Jacqueline175,Middlename3,Lastname525,9222016,,1234568413,,email155@umbc.edu,134 Street Address,,Pikesville,MD,Maryland,US,United States,21208,,424 permanent street address,,Pikesville,MD,Maryland,US,United States,21208,Female,No,,,,U.S. Citizen,3/29/1989,United States,,,,,,,,,,,,,,,,,,,,,,,Yes,,,,,,,,,,,,,No,No,,,,,,,,,,,,,,,,,UNIVERSITY OF MARYLAND - BALTIMORE COUNTY,,,,,,,,,,,,,,,reffirstname522,reffirstname525,,,,,,,,,,,,,,referencelastname525,referencelastname525,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,100000524,,,,,,,Native,,,,,,,,,,,,,,,English,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,';



    Pattern p = Pattern.compile('(,"\\[a-zA-Z 0-9,.:\'\\]*)(\\[\r\n\\]*)(\\[a-zA-Z 0-9,.\':\\]*")');
    Matcher m = p.matcher( res );




        System.debug(LoggingLevel.ERROR,'DEBUG before while loop m.matches = ' + m.matches());
        while (m.find()) {
            res = res.replace(m.group(), m.group(1) + ' ' + m.group(3));


            System.debug(LoggingLevel.ERROR,'DEBUG group()=' + m.group());
            System.debug(LoggingLevel.ERROR, 'DEBUG group(1)=' + m.group(1));
            System.debug(LoggingLevel.ERROR, 'DEBUG group(2)=' + m.group(2));
            System.debug(LoggingLevel.ERROR, 'DEBUG group(3)=' + m.group(3));
        }


        System.debug(LoggingLevel.ERROR, 'DEBUG: ApplicationImportProcessor.cleanLfInCellCsvData res=' + res);

        CsvReader reader = new CsvReader(res, ',');

 
Best Answer chosen by vlr
Alain CabonAlain Cabon
You need to activate the multiline mode first in your pattern.

Multiline mode can be enabled via the embedded flag expression (?m).   [little kown syntax in java]

Here for an example: http://salesforce.stackexchange.com/questions/11195/unable-to-use-multiline-with-pattern

In Apex, Patterns and Matchers, as well as regular expressions, are based on their counterparts in Java. See http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/regex/Pattern.html.

Alain

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Vir,

As per Salesforce documentation(https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm), there is a function called unescapeJava() available in String class which returns a String whose Java literals are unescaped. Literals unescaped include escape sequences for quotes (\") and control characters, such as tab (\t), and carriage return (\n). I hope this can solve your issue.

Regards,
Nagendra.P
Alain CabonAlain Cabon
You need to activate the multiline mode first in your pattern.

Multiline mode can be enabled via the embedded flag expression (?m).   [little kown syntax in java]

Here for an example: http://salesforce.stackexchange.com/questions/11195/unable-to-use-multiline-with-pattern

In Apex, Patterns and Matchers, as well as regular expressions, are based on their counterparts in Java. See http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/util/regex/Pattern.html.

Alain
This was selected as the best answer