0

I wish to ask, is it possible to use regexes as delimiters in PapaParse? Something like:

Papa.parse(string,
 {
    delimiter:regex
 }
);

I am trying to match a specific pattern of CSVs like so:

/([A-Za-z]{2}[0-9]+,?)/g

i.e. I want exactly 2 letters, any amount of numbers, and a comma (or not, in the case of the last element).

Since string.split has a wonderful habit of returning anything but null when nothing matches regex patterns, I was hoping that my answer would lie in PapaParse. If this is not possible, then I would do something more long winded, but hopefully I can be laz-... efficent this time. :)

Trying to do the following:

Papa.parse('ACB5,dsa',{delimiter:'[A-Za-z]{2}[0-9]+,?'});

Results in

["ACB5","dsa"]

Thank you for your time.

edit

Trying out the regex on regexr.com shows that it works with values like

AB544444444444,BC5, aa5,

At this point, I realize that this was actually a dozy question, considering how a delimiter is the thing that separates what you want to break up.

I'm writing the longer winded version now, so I'll stick that up soon

PiMan
  • 55
  • 10
  • I don't know anything about PapaParse, but I do know that it's called a `delimiter` - could that have anything to do with the problem? Could you provide a few test cases (input and desired output), a regex might not be that difficult to construct if we know what you're really trying to do. – Tim Pietzcker Jul 03 '15 at 16:11
  • Thanks Tim, I fixed that mistake in the question. I have nailed down the regex using regexr.com, so I am sure it is correct, – PiMan Jul 03 '15 at 16:15
  • So it looks like your delimiter needs to be a... comma. – Matt Jul 03 '15 at 19:48

1 Answers1

0

As Matt (and common sense) rightly say, yes, The delimiter is just ye olde comma. I was looking for a way to separate the results based on a regex, which past me had thought would have some similarity to how string.split works. This is the snippet I was trying to shrink down.

    var result = null;
    var regex = /([A-Za-z]{2}[0-9]+,?)/g;       //Any two letters, a number, and a space
$(document).ready( function() {
  $('#inputGraphText').on('input', function(e){ //on text input...
    result = $(this).val().split(',');          //split using the complex delimiter ','. Also adds a few "" elements to the array for some reason.
    var tidy = new Array();
    result.forEach(function(elem){
        if(elem.search(regex) > -1){
          tidy.push(elem.replace('/[, ]/g',''));//Tidy up the result
        }
    });

    $('#first').html(tidy);                     //place to print out the tidied results
  })
});

Obviously , this is not terribly schnazzy (and completely misses out on using PapaParse), but it is what I originally set out to do.

Any better alternatives will take pride of place, but for now, this is fine.

My apologies for the confusion.

PiMan
  • 55
  • 10