Need help with javascript

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I have several hundred entries in a csv file. I'm running them through an RE so that the entries are paired in 2 for each line. I'm having a lof of trouble with the regular expression to do this.

The entries are like this:

Code: [ Select ]
ART 223 - Watercolor III,
"Concentrates on the advanced study of subject development, form, color, and theme in watercolor. Three credits.",
  1. ART 223 - Watercolor III,
  2. "Concentrates on the advanced study of subject development, form, color, and theme in watercolor. Three credits.",


Essentially, the only pattern to go by is this:
  • First entry of pair will always begin with 3-4 capitol letters followed by a space, a dash, another space, and then any number of words, numbers or symbols except a comma.
  • First entry will end with a comma followed by a carriage return.
  • Second entry will sometimes begin with a quotation, followed by a sentence containing any number of words, numbers, or symbols including commas.
  • Second entry will sometimes include a closing quotation at the end of the entry.
  • Second entry will end with a comma followed by a carriage return.
The result I want to have after a replace should look like this:
Code: [ Select ]
ART 223 - Watercolor III,"Concentrates on the advanced study of subject development, form, color, and theme in watercolor. Three credits.",

Essentially, the resulting pattern will follow these rules:
  • Removes the ending carriage return on the first entry.
  • Ensures a quotation at the beginning of the second entry.
  • Ensures a quotation at the end of the second entry.
  • Ensure that the second entry ends with a comma, followed by a carriage return.

here's what I have so far, but it's gotten pretty messy while I've been messing with it so it looks pretty bad.

Code: [ Select ]
regexp match: ([a-zA-Z]{3,4}\s[0-9]{3,4}\s-\s[\sa-z$`A-Z0-9\/\\\&\[\]\-\.\:\(\)]*\,)(\r)([\sa-zA-Z0-9\/\\\&\[\]\-\.\:\",\(\)\'\;\,]*)
Regexp replace: $1$3
  1. regexp match: ([a-zA-Z]{3,4}\s[0-9]{3,4}\s-\s[\sa-z$`A-Z0-9\/\\\&\[\]\-\.\:\(\)]*\,)(\r)([\sa-zA-Z0-9\/\\\&\[\]\-\.\:\",\(\)\'\;\,]*)
  2. Regexp replace: $1$3

Any ideas?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Okay, I figured that part out.
Code: [ Select ]
find: ([A-Z]{3,4})(\s[0-9]{3,4})(\s-\s[\sa-zA-Z0-9\/\\\&\[\]\-\.\:\(\)\']*)(\,)(\r)(\"*)([\sa-zA-Z0-9\/\\\&\[\]\-\.:,\(\)';,]*)("*)(,)(\r|\n)
replace: "6","z","$1$2$3","$7","1"\r
  1. find: ([A-Z]{3,4})(\s[0-9]{3,4})(\s-\s[\sa-zA-Z0-9\/\\\&\[\]\-\.\:\(\)\']*)(\,)(\r)(\"*)([\sa-zA-Z0-9\/\\\&\[\]\-\.:,\(\)';,]*)("*)(,)(\r|\n)
  2. replace: "6","z","$1$2$3","$7","1"\r


output:
Code: [ Select ]
"6","z","AAA 101 - College 101:Student Experience","Introduces students to college culture and prepares them for the challenges they will face in higher education. Through a series of interactive seminars, students discover learning in a multicultural environment and use college and community resources to attain education and career goals. One credit.","1"

It's a little different than my initial needed format, but the changes were necessary to set up for a premade parsing engine.

My next question:
I need to change "z" to a number. Basically loop through each match and replace the "z" with an incremental value. Anyone know how to do this?
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

okay, getting close...

I've got this code:
JAVASCRIPT Code: [ Select ]
var text = document.getElementById("text").innerHTML;
text = text.replace(/"z"/g, function(n){ return ++n });
document.write(text);
 
  1. var text = document.getElementById("text").innerHTML;
  2. text = text.replace(/"z"/g, function(n){ return ++n });
  3. document.write(text);
  4.  

The issue is that I don't know how to pass values through the function. Seriously...this has been an all day, non-stop project to get something done that was supposed to save me time on the actual project. Please god someone help me.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

Okay, got it. remove the parameter in the function. Add a variable for n. This is definitely something someone could have answered. Very simple and I was just too tired and mushy-brained when I posted it. Can't believe I didn't get an answer from you guys for something this easy. I guess the forum really is quiet lately.

Code: [ Select ]
var text = document.getElementById("text").innerHTML;
var n = 0;
text = text.replace(/"z"/g, function(){ return '"' + (++n) + '"' });
document.write(text);
  1. var text = document.getElementById("text").innerHTML;
  2. var n = 0;
  3. text = text.replace(/"z"/g, function(){ return '"' + (++n) + '"' });
  4. document.write(text);
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I had just went through everything and noticed you already fixed it. Yeah things are slow around here, but people are still reading (at least I read everything). Didn't seem to complicated to fix. So is everything the way you want now? Was this all so you could reformat your CSV file? Will you be using what you wrote often? The main reason I ask, is if I were in your shoes I probably would have used the regex engine in Notepad++ to do all of this much quicker.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

It's the way I want it, yes it was for formatting my csv, I'll probably use it fairly often. I'm not familiar with the regex engine in notepad++. I was using this online tool to get the lines reorganized, and then the above function to replace the z with the incremented numbers. Is notepad++ regex engine similar to this?
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

Yes very similar, its all based on the same regex rules. Inside Notepad++ I usually hit Ctrl-R which will popup a "Find/Replace" box. Before you type in your regex to use, make sure that you select the boxes "Match case" and "Regular Expr" so that you can use your regexes correctly.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 184 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.