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]);
})
6
  • 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" ] Commented Dec 9, 2016 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 Commented Dec 9, 2016 at 16:29
  • Should work just fine. 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.) Commented Dec 9, 2016 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+? Commented Dec 9, 2016 at 16:49
  • Thats it I needed to use \s+ Commented Dec 9, 2016 at 16:59

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

The reason the OP added \d? is probably to consider 100, which wouldn't work here. To be confirmed by said OP. ;)
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})
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.
nope, added another pic with example on a regex site working fine. Post your javascript and maybe I can see what is going on
I updated the question to show what works and what dosn't work
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.