0

I have a list of Student ID's along with the results, three test results, each student recieved. I have created a fileReader to read text from a local document and stored the results in a variable. I have a regex for pulling the information I need, which should work but returns null.

The information is stored as;

C00695260
93
76
86 

The regex im trying to use is, /C\d{8}\s\d{2}\d?\s\d{2}\d?\s\d{2}\d?/g which highlights what I want in sublime but not in the program, or browser console. It works as expected until after, /C\d{8}\s\d{2}\d?\s/g, but I can't work out why. This is my first post so sorry if I'm doing anything wrong;

This dosn't work

var textIn;
var r =/B\d{8}\s\d{2}\d?\s\d{2}\d?\s\d{2}\d?/g;
var print;
//crete a function the read listen fot the file to change
document.getElementById('openFile').addEventListener('change', function(){
    var reader = new FileReader();
    reader.onload = function(){

        textIn = this.result;

        print = textIn.match(r);

        document.getElementById('Filecontents').textContent = print;
    }
    reader.readAsText(this.files[0]);
})

This works!

var textIn;
var r =/(B\d{8})\s/g;
var print;
//crete a function the read listen fot the file to change
document.getElementById('openFile').addEventListener('change', function(){
    var reader = new FileReader();
    reader.onload = function(){

        textIn = this.result;

        print = textIn.match(r);

        document.getElementById('Filecontents').textContent = print;
    }
    reader.readAsText(this.files[0]);
})
KSwaby
  • 1
  • 1
  • In Firefox my console is OK with your try: `"C00695260 93 76 86".match(/C\d{8}\s\d{2}\d?\s\d{2}\d?\s\d{2}\d?/g)` returns `Array [ "C00695260 93 76 86" ]` – Stock Overflaw Dec 09 '16 at 16:15
  • I have read in an external txt file and saved the results in a variable, It has over 180 people and there results, the code works if I save one persons results in the console and if I run match with the regex code untill after the first whitespace – KSwaby Dec 09 '16 at 16:29
  • [Should work just fine](https://jsfiddle.net/ehbeo18a/). Also, you might want to use the regex as provided by jimplode to make sure you target all numbers, ranging from 0-100. (Efficiency wise it could be better, but it's at least effective.) – Bram Vanroy Dec 09 '16 at 16:43
  • Ha I see you edited your post so you have actually newlines... so it might be due to having several non-printable characters whereas `\s` considers only one char. Try replacing every `\s` with `\s+`? – Stock Overflaw Dec 09 '16 at 16:49
  • Thats it I needed to use \s+ – KSwaby Dec 09 '16 at 16:59
  • A newline on Windows is represented as the character sequence `\r\n`, that's why you needed to target multiple white-space characters. [More info](http://stackoverflow.com/questions/7013034/does-windows-carriage-return-r-n-consist-of-two-characters-or-one-character). – Bram Vanroy Dec 09 '16 at 17:02

1 Answers1

1

you want something like this: (C\d{8})\s(\d{1,3})\s(\d{1,3})\s(\d{1,3})

(C\d{8}) = A numbered captured group looking for a "C" following by 8 digits

\s = white space

(\d{1,3}) = A numbered captured group looking for 1 - 3 digits

repeat

enter image description here

enter image description here

jimplode
  • 3,474
  • 3
  • 24
  • 42
  • The reason the OP added `\d?` is probably to consider `100`, which wouldn't work here. To be confirmed by said OP. ;) – Stock Overflaw Dec 09 '16 at 16:12
  • 2
    if you need to cater for that do you also need to cater for just a single digit? If so you should use (\d{1,3}) – jimplode Dec 09 '16 at 16:14
  • Yes the results are out of 100. The regex above dosn't work either, could it be a javascript thing? The regex works until after the first whitespace. – KSwaby Dec 09 '16 at 16:24
  • nope, added another pic with example on a regex site working fine. Post your javascript and maybe I can see what is going on – jimplode Dec 09 '16 at 16:38
  • I updated the question to show what works and what dosn't work – KSwaby Dec 09 '16 at 16:55
  • I created a fiddle to show this working, will look at your update now https://jsfiddle.net/mdjpvnqr/ – jimplode Dec 09 '16 at 16:57