Activity Forums Salesforce® Discussions Regex too complicated

  • Gourav

    Member
    May 30, 2016 at 11:49 am

    Not a catch per say (IIRC you cannot catch it, especially if Catch Exception did not work) but will solve the problem of regex to complex, use a custom iterator:

    Class

    public with sharing class Utility_RowIterator implements Iterator<String>, Iterable<String>
    {
    private String m_Data;
    private Integer m_index = 0;
    private String m_rowDelimiter = '\n';

    public Utility_RowIterator(String fileData)
    {
    m_Data = fileData;
    }
    public Utility_RowIterator(String fileData, String rowDelimiter)
    {
    m_Data = fileData;
    m_rowDelimiter = rowDelimiter;
    }

    public Boolean hasNext()
    {
    return m_index < m_Data.length() ? true : false;
    }
    public String next()
    {
    Integer key = m_Data.indexOf(m_rowDelimiter, m_index);

    if (key == -1)
    key = m_Data.length();

    String row = m_Data.subString(m_index, key);
    m_index = key + 1;
    return row;
    }
    public Iterator<String> Iterator()
    {
    return this;
    }
    }

     

    Code

    Utility_RowIterator r = New Utility_RowIterator(csv,'\n'); //Replace \n with whatever delineates your row

    String firstRow;
    if(r.hasNext()) firstRow = r.next();

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos