I wonder if it's possible to use the apex public String[] split(String regExp) method to split a string by the newline characters. The string being a csv file created on the Windows platform.

I tried several apex expressions in different forms and shapes, but it either doesn't work for me or it probably leaves the carriage return symbol within the resulting strings (if I try to match a resulting string against another clean string it should match, they don't match).

  • Can you provide more detail about where you are getting your input from, possibly including an example? – Adrian Larson Feb 16 '16 at 17:19
  • @AdrianLarson I'm getting the input from the CSV file created with Microsoft Excel. – Andrew Feb 16 '16 at 17:22
  • 1
    You can use \\n or \\r\\n to split in windows – Anurag Bhardwaj Feb 16 '16 at 17:25
  • Did you ever get this working? – Adrian Larson Mar 9 '16 at 21:15

Try \n:


If your input uses \r\n, that is another common option.

  • I have already tried those two. – Andrew Feb 16 '16 at 17:18
  • It accepts a regex, which in apex needs an extra escape. E.g. \\r\\n – ca_peterson Feb 17 '16 at 4:07
  • @ca_peterson Yeah that's what I thought too, but if I debug 'a\nb\nc\nd'.split('\n') I get a list with the correct 4 elements. Why does that work? – Adrian Larson Feb 17 '16 at 4:14
  • Good question, I'm curious myself why this works. My understanding is that it shouldn't, as Andrejs observed. – ca_peterson Feb 17 '16 at 4:24

In windows you can use split method in other format. Use below example





Since Salesforce implements Java Pattern class, the meta-characters that have a special meaning are needs to be escaped. Meta <([{\^-=$!|]})?*+.>.

Here three back slashes are used instead of one is because the meta-character needs to be escaped with a "\" but since "\" is also a meta-character, you need to escape it again with another "\".

Useful Links: Java Regex Tutorial | String.split Blogpost

  • Since you're the first one to realize it...might be worth explaining why you need to escape \ characters. – Adrian Larson Feb 16 '16 at 17:59
  • 2
    And this is not just for windows, btw. – Sebastian Kessel Feb 16 '16 at 18:41
  • In windows, backslash character () is know as escape sequence. Windows use the file system in backslash character. So whenever we need to use any combination of backslash, we will put extra backslash so that window system can understand it. – Anurag Bhardwaj Feb 16 '16 at 18:44
  • Triple backslash doesn't work for me either. But I think that the core issue here is that I should not use the public String[] split(String regExp) method when reading and processing a file, as the behavior may be unpredictable. – Andrew Feb 17 '16 at 9:17
  • But is there any other way? – Andrew Feb 17 '16 at 9:53

Note if the string object is too large you will get the exception Regex too complicated. Your regex is actually fine, but you need to truncate the string. In this case you can do something like the following:

Blob blobValue = (Blob)record.get(blobField);
// Truncate string then split on newline, limiting to 11 entries
List<String> preview = blobValue.toString().substring(0,1000).split('\n', 11);
// Remove the last entry, because The list’s last entry contains all input beyond the last matched delimiter.
// In my use-case, I needed to return a string, and String.join() works as the reverse of split()    
return String.join(preview, '\n');

You should only need to supply '\n' as the regular expression. The backslash is interpreted within the String as starting an escape sequence resulting in a single code point (will just call this a character). Since the split method takes a string as the regular expression, this single character will be taken as the delimiter for the split. AFAIK the {newline} character has no significance in the Apex version of regular expressions so no need to escape it like '\(' or '\['.

BTW processing complex CSVs can get extremely difficult (maybe impossible) using regular expressions. A "cell" can contain {newline} characters as long as the cell value is surrounded by double quotes, and in fact it can contain escaped double quotes making this a very messy topic. As of February 2021 I found the String "escapeCsv" method to encode a cell, but no corresponding "unescapeCsv" for either a cell or more importantly a file. Also Windows often uses '\r\n' rather than just '\n', while some old Mac programs would use only '\r'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.