Find all sequences of letters and merge, leave the six numbers alone

Viewed 55

So I encountered a problem dataset, it contains a variable sequence of letters, followed by a sequence of six numbers.

Input Datastream

"A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80", "B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", "D", "Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F", "2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08", "2.12", "48.00", "82.60", "8.3", "2.47", "Gf", "H", "I", "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "J", "K", "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "L", "M", "1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06", "2.05", "41.00", "80.60", "9.0", "4.09", "O", "P", "1.86", "2.04", "48.00", "81.60", "8.6", "2.60", "Qst", "R", "1.95", "2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02", "40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00", "81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60", "9.2", "2.40", "V", "W", "C", "1.98", "1.97", "40.00", "82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80", "9.6", "2.08", "Z", "a", "bi", "1.90", "1.95", "42.00", "84.20", "9.6", "1.69" 

Question:

I want to merge each of the letter groupings, in the text data set, I have encountered problems creating the correct regex expression to strip out the ", " between the letters. My thought was to focus on the ", between the letters and replace them with blanks, thus get the letters to merge.

((?:[A-Za-z]*)(", ")(?:[A-Za-z]*))*

The intent with the above was to create a named middle capture, ignore the outer and replace the middle capture with empty. What am I missing? All help will be graciously received and be much appreciated.

Expected Result:

"Aa", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80", "B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", "DEt", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", "F", "2.05", "2.15", "43.00", "82.20", "8.4", "3.11", "Bc", "2.08", "2.12", "48.00", "82.60", "8.3", "2.47", "GfHI", "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", "JK", "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", "LM", "1.89", "2.07", "45.00", "83.80", "9.0", "1.84", "N", "2.06", "2.05", "41.00", "80.60", "9.0", "4.09", "OP", "1.86", "2.04", "48.00", "81.60", "8.6", "2.60", "QstR", "1.95", "2.03", "44.00", "82.80", "8.8", "1.40", "S", "2.03", "2.02", "40.00", "81.40", "8.2", "1.74", "T", "1.95", "2.01", "43.00", "81.80", "9.0", "2.30", "Unh", "1.96", "2.00", "44.00", "82.60", "9.2", "2.40", "VWC", "1.98", "1.97", "40.00", "82.00", "8.1", "1.15", "Yu", "1.90", "1.96", "41.00", "82.80", "9.6", "2.08", "Zabi", "1.90", "1.95", "42.00", "84.20", "9.6", "1.69"

Simplified view (with line breaks)

Initial Dataset

"A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80", 
"B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", 
"D", "Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", 
"F", "2.05", "2.15", "43.00", "82.20", "8.4", "3.11", 
"Bc", "2.08", "2.12", "48.00", "82.60", "8.3", "2.47", 
"Gf", "H", "I", "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", 
"J", "K", "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", 
"L", "M", "1.89", "2.07", "45.00", "83.80", "9.0", "1.84",
"N", "2.06", "2.05", "41.00", "80.60", "9.0", "4.09", 
"O", "P", "1.86", "2.04", "48.00", "81.60", "8.6", "2.60",
"Qst", "R", "1.95", "2.03", "44.00", "82.80", "8.8", "1.40", 
"S", "2.03", "2.02", "40.00", "81.40", "8.2", "1.74", 
"T", "1.95", "2.01", "43.00", "81.80", "9.0", "2.30", 
"Unh", "1.96", "2.00", "44.00", "82.60", "9.2", "2.40",
"V", "W", "C", "1.98", "1.97", "40.00", "82.00", "8.1", "1.15", 
"Yu", "1.90", "1.96", "41.00", "82.80", "9.6", "2.08", 
"Z", "a", "bi", "1.90", "1.95", "42.00", "84.20", "9.6", "1.69"

Expected Result:

“Aa”, “2.07", “2.35”, “39.00", “82.20”, “8.8", “3.80”,
“B”, “2.26”, “2.25", “40.00”, “80.80", “8.1”, “1.86",
“DEt”, “2.07", “2.22”, “41.00", “83.80”, “8.8", “3.87”,
“F”, “2.05”, “2.15", “43.00”, “82.20", “8.4”, “3.11"
"Bc", "2.08", "2.12", "48.00", "82.60", "8.3", "2.47", 
"GfHI", "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", 
"JK", "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", 
"LM", "1.89", "2.07", "45.00", "83.80", "9.0", "1.84", 
"N", "2.06", "2.05", "41.00", "80.60", "9.0", "4.09",
"OP", "1.86", "2.04", "48.00", "81.60", "8.6", "2.60",
"QstR", "1.95", "2.03", "44.00", "82.80", "8.8", "1.40",
"S", "2.03", "2.02", "40.00", "81.40", "8.2", "1.74",
"T", "1.95", "2.01", "43.00", "81.80", "9.0", "2.30",
"Unh", "1.96", "2.00", "44.00", "82.60", "9.2", "2.40",
"VWC", "1.98", "1.97", "40.00", "82.00", "8.1", "1.15",
"Yu", "1.90", "1.96", "41.00", "82.80", "9.6", "2.08",
"Zabi", "1.90", "1.95", "42.00", "84.20", "9.6", "1.69"
3 Answers

You want (?![A-Z])(", ")(?=[A-Z]) with the g, m, and i flags.

const regex = /(?![A-Z])(", ")(?=[A-Z])/gmi;
const input = `"A", "a", "2.07", "2.35", "39.00", "82.20", "8.8", "3.80", 
"B", "2.26", "2.25", "40.00", "80.80", "8.1", "1.86", 
"D", "Et", "2.07", "2.22", "41.00", "83.80", "8.8", "3.87", 
"F", "2.05", "2.15", "43.00", "82.20", "8.4", "3.11", 
"Bc", "2.08", "2.12", "48.00", "82.60", "8.3", "2.47", 
"Gf", "H", "I", "2.08", "2.10", "46.00", "82.20", "8.1", "2.90", 
"J", "K", "1.95", "2.08", "38.00", "83.40", "8.7", "1.63", 
"L", "M", "1.89", "2.07", "45.00", "83.80", "9.0", "1.84",
"N", "2.06", "2.05", "41.00", "80.60", "9.0", "4.09", 
"O", "P", "1.86", "2.04", "48.00", "81.60", "8.6", "2.60",
"Qst", "R", "1.95", "2.03", "44.00", "82.80", "8.8", "1.40", 
"S", "2.03", "2.02", "40.00", "81.40", "8.2", "1.74", 
"T", "1.95", "2.01", "43.00", "81.80", "9.0", "2.30", 
"Unh", "1.96", "2.00", "44.00", "82.60", "9.2", "2.40",
"V", "W", "C", "1.98", "1.97", "40.00", "82.00", "8.1", "1.15", 
"Yu", "1.90", "1.96", "41.00", "82.80", "9.6", "2.08", 
"Z", "a", "bi", "1.90", "1.95", "42.00", "84.20", "9.6", "1.69"`;
const output = input.replace(regex, '');
console.log(output);

Regex:

("[A-Za-z]*)(", ")([A-Za-z]*")

Replace with

\1\3

Test here.

Note: if you have more than 2 groups to join, just run the search and replace several times - until no replacement is made.

Seems like you can look for:

(?<!\d)", "(?!\d)

And replace with nothing. Check here online.


  • (?<!\d) - Negative lookbehind to prevent a digit.
  • ", " - A literal combination of quotes, comma and space.
  • (?!\d) - Negative lookahead to prevent a digit.
Related