Adam Chubbuck
Adam Chubbuck

Reputation: 1690

Using regular expression to capture the name of the speaker and the comment

I would like to capture the name of the speaker and the comment that was made. I've tried using several different variations of ([-]{5,}\s\S+), but this doesn't seem to do the tricky. Ideally, I would like to end up with the following:

var data = [
    {
        name: "Operator",
        comment: "Ladies and gentlemen, thank you for standing by. Welcome to the 3M First Quarter Earnings Conference Call. (Operator Instructions) As a reminder, this conference is being recorded, Thursday, April 25, 2019. I would now like to turn the call over to Bruce Jermeland, Vice President of Investor Relations at 3M."
    },
    {
        name: "Bruce Jermeland,  3M Company - Director of IR",
        comment: "Thank you, and good morning, everyone. Welcome to our first quarter 2019 business review. With me today are Mike Roman, 3M's Chief Executive Officer; and Nick Gangestad, our Chief Financial Officer. Mike and Nick will make some formal comments, and then we'll take your questions. Please note that today's earnings release and slide presentation accompanying this call are posted on our Investor Relations website at 3m.com under the heading, Quarterly Earnings. Please turn to Slide 2. Before we begin, let me remind you to mark your calendars for our upcoming earnings calls on July 25 and October 24. Please take a moment to read the forward-looking statement on Slide 3. During today's conference call, we will make certain predictive statements that reflect our current views about 3M's future performance and financial results. These statements are based on certain assumptions and expectations of future events that are subject to risks and uncertainties. Item 1A of our most recent Form 10-K lists some of the most important risk factors that could cause actual results to differ from our predictions. Finally, please note that throughout today's presentation, we will be making references to certain non-GAAP financial measures, in particular, measures which exclude the impact of the Tax Cuts and Jobs Act and significant litigation-related charges. Reconciliations of the non-GAAP measures can be found in the appendix of today's presentation and press release. Please turn to Slide 4, and I'll hand it off to Mike. Mike?"
    },
    ...
]

How can I accomplish this?

var text = `Q1 2019 3M Co Earnings Call

ST. PAUL Apr 27, 2019 (Thomson StreetEvents) -- Edited Transcript of 3M Co earnings conference call or presentation Thursday, April 25, 2019 at 1:00:00pm GMT

TEXT version of Transcript

================================================================================
Corporate Participants
================================================================================
   *  Bruce Jermeland
      3M Company - Director of IR
   *  Michael F. Roman
      3M Company - CEO & Director
   *  Nicholas C. Gangestad
      3M Company - Senior VP & CFO

================================================================================
Conference Call Participants
================================================================================
   *  Andrew Alec Kaplowitz
      Citigroup Inc, Research Division - MD and U.S. Industrial Sector Head
   *  Andrew Burris Obin
      BofA Merrill Lynch, Research Division - MD
   *  Charles Stephen Tusa
      JP Morgan Chase & Co, Research Division - MD
   *  Deane Michael Dray
      RBC Capital Markets, LLC, Research Division - Analyst
   *  John Fred Walsh
      Crédit Suisse AG, Research Division - Director
   *  John George Inch
      Gordon Haskett Research Advisors - MD & Senior Analyst of Multi-Industrials
   *  Joseph Alfred Ritchie
      Goldman Sachs Group Inc., Research Division - VP & Lead Multi-Industry Analyst
   *  Joshua Charles Pokrzywinski
      Morgan Stanley, Research Division - Equity Analyst
   *  Julian C.H. Mitchell
      Barclays Bank PLC, Research Division - Research Analyst
   *  Laurence Alexander
      Jefferies LLC, Research Division - VP & Equity Research Analyst
   *  Nigel Edward Coe
      Wolfe Research, LLC - MD & Senior Research Analyst
   *  Scott Reed Davis
      Melius Research LLC - Founding Partner, Chairman, CEO & Research Analyst of Multi-Industry Research

================================================================================
Presentation
--------------------------------------------------------------------------------
Operator   [1]
--------------------------------------------------------------------------------
  Ladies and gentlemen, thank you for standing by. Welcome to the 3M First Quarter Earnings Conference Call. (Operator Instructions) As a reminder, this conference is being recorded, Thursday, April 25, 2019.
 I would now like to turn the call over to Bruce Jermeland, Vice President of Investor Relations at 3M.
--------------------------------------------------------------------------------
 Bruce Jermeland,  3M Company - Director of IR   [2]
--------------------------------------------------------------------------------
  Thank you, and good morning, everyone. Welcome to our first quarter 2019 business review. With me today are Mike Roman, 3M's Chief Executive Officer; and Nick Gangestad, our Chief Financial Officer. Mike and Nick will make some formal comments, and then we'll take your questions.
 Please note that today's earnings release and slide presentation accompanying this call are posted on our Investor Relations website at 3m.com under the heading, Quarterly Earnings. Please turn to Slide 2.
 Before we begin, let me remind you to mark your calendars for our upcoming earnings calls on July 25 and October 24. Please take a moment to read the forward-looking statement on Slide 3.
 During today's conference call, we will make certain predictive statements that reflect our current views about 3M's future performance and financial results. These statements are based on certain assumptions and expectations of future events that are subject to risks and uncertainties. Item 1A of our most recent Form 10-K lists some of the most important risk factors that could cause actual results to differ from our predictions.
 Finally, please note that throughout today's presentation, we will be making references to certain non-GAAP financial measures, in particular, measures which exclude the impact of the Tax Cuts and Jobs Act and significant litigation-related charges. Reconciliations of the non-GAAP measures can be found in the appendix of today's presentation and press release.
 Please turn to Slide 4, and I'll hand it off to Mike. Mike?
--------------------------------------------------------------------------------
Operator   [87]
--------------------------------------------------------------------------------
  Ladies and gentlemen, that does conclude the conference call for today. We thank you for your participation and ask that you please disconnect your lines.`;
  
console.log(text);

Upvotes: 1

Views: 89

Answers (3)

Slai
Slai

Reputation: 22876

Seems like you can just split by /-{5,}/ and trim the parts :

var text = `Q1 2019 3M Co Earnings Call

ST. PAUL Apr 27, 2019 (Thomson StreetEvents) -- Edited Transcript of 3M Co earnings conference call or presentation Thursday, April 25, 2019 at 1:00:00pm GMT

TEXT version of Transcript

================================================================================
Corporate Participants
================================================================================
   *  Bruce Jermeland
      3M Company - Director of IR
   *  Michael F. Roman
      3M Company - CEO & Director
   *  Nicholas C. Gangestad
      3M Company - Senior VP & CFO

================================================================================
Conference Call Participants
================================================================================
   *  Andrew Alec Kaplowitz
      Citigroup Inc, Research Division - MD and U.S. Industrial Sector Head
   *  Andrew Burris Obin
      BofA Merrill Lynch, Research Division - MD
   *  Charles Stephen Tusa
      JP Morgan Chase & Co, Research Division - MD
   *  Deane Michael Dray
      RBC Capital Markets, LLC, Research Division - Analyst
   *  John Fred Walsh
      Crédit Suisse AG, Research Division - Director
   *  John George Inch
      Gordon Haskett Research Advisors - MD & Senior Analyst of Multi-Industrials
   *  Joseph Alfred Ritchie
      Goldman Sachs Group Inc., Research Division - VP & Lead Multi-Industry Analyst
   *  Joshua Charles Pokrzywinski
      Morgan Stanley, Research Division - Equity Analyst
   *  Julian C.H. Mitchell
      Barclays Bank PLC, Research Division - Research Analyst
   *  Laurence Alexander
      Jefferies LLC, Research Division - VP & Equity Research Analyst
   *  Nigel Edward Coe
      Wolfe Research, LLC - MD & Senior Research Analyst
   *  Scott Reed Davis
      Melius Research LLC - Founding Partner, Chairman, CEO & Research Analyst of Multi-Industry Research

================================================================================
Presentation
--------------------------------------------------------------------------------
Operator   [1]
--------------------------------------------------------------------------------
  Ladies and gentlemen, thank you for standing by. Welcome to the 3M First Quarter Earnings Conference Call. (Operator Instructions) As a reminder, this conference is being recorded, Thursday, April 25, 2019.
 I would now like to turn the call over to Bruce Jermeland, Vice President of Investor Relations at 3M.
--------------------------------------------------------------------------------
 Bruce Jermeland,  3M Company - Director of IR   [2]
--------------------------------------------------------------------------------
  Thank you, and good morning, everyone. Welcome to our first quarter 2019 business review. With me today are Mike Roman, 3M's Chief Executive Officer; and Nick Gangestad, our Chief Financial Officer. Mike and Nick will make some formal comments, and then we'll take your questions.
 Please note that today's earnings release and slide presentation accompanying this call are posted on our Investor Relations website at 3m.com under the heading, Quarterly Earnings. Please turn to Slide 2.
 Before we begin, let me remind you to mark your calendars for our upcoming earnings calls on July 25 and October 24. Please take a moment to read the forward-looking statement on Slide 3.
 During today's conference call, we will make certain predictive statements that reflect our current views about 3M's future performance and financial results. These statements are based on certain assumptions and expectations of future events that are subject to risks and uncertainties. Item 1A of our most recent Form 10-K lists some of the most important risk factors that could cause actual results to differ from our predictions.
 Finally, please note that throughout today's presentation, we will be making references to certain non-GAAP financial measures, in particular, measures which exclude the impact of the Tax Cuts and Jobs Act and significant litigation-related charges. Reconciliations of the non-GAAP measures can be found in the appendix of today's presentation and press release.
 Please turn to Slide 4, and I'll hand it off to Mike. Mike?
--------------------------------------------------------------------------------
Operator   [87]
--------------------------------------------------------------------------------
  Ladies and gentlemen, that does conclude the conference call for today. We thank you for your participation and ask that you please disconnect your lines.`;

var data = [], parts = text.split(/-{5,}/);

for (var i = 1; i < parts.length; i += 2)
  data.push({ name: parts[i].split('[')[0].trim(), comment: parts[i + 1].trim() });

console.log(data);

Upvotes: 1

The fourth bird
The fourth bird

Reputation: 163457

You could use a capturing group and for the match of the speaker use a character class and specify what characters you would allow for the name and repeat that with a space in between.

Then match the line with only hyphens followed by using a second capturing group matching the following content until you encounter the next line that starts with a hyphen. (Or multiple if you want to make it more specific.

^[ \t]*(\w+(?:[ \t]+[\w,-]+)*)[\t ]+\[\d+\]\r?\n-+\s+((?:(?:\r?\n)?(?!-).*)*)

Explanation

  • ^ Start of string
  • [ \t]* Match 0+ tabs/spaces
  • ( Capture group 1
    • \w+(?:[ \t]+[\w,-]+)* Match 1+ word char and repeat 0+ times matching 1+ tabs/spaces and a character class where you could specify what you would allow to match.
  • ) Close group 1
  • [\t ]+\[\d+\] Match 1+ tabs/spaces, the 1+ digits between [..]
  • \r?\n-+\s+ Match newline, 1+ times a hyphen followed by 1+ times a whitespace char (which will also match the prepended spaces before the start of the comment)
  • ( Capture group 2
    • (?: Non capturing group
      • (?:\r?\n)?(?!-).* Match optional newline, assert what is on the right is not a hyphen, then match the rest of the line.
    • )* Close non capturing group and repeat 0+ times
  • )Close group 2

Regex demo

var str = `Q1 2019 3M Co Earnings Call

ST. PAUL Apr 27, 2019 (Thomson StreetEvents) -- Edited Transcript of 3M Co earnings conference call or presentation Thursday, April 25, 2019 at 1:00:00pm GMT

TEXT version of Transcript

================================================================================
Corporate Participants
================================================================================
   *  Bruce Jermeland
      3M Company - Director of IR
   *  Michael F. Roman
      3M Company - CEO &amp; Director
   *  Nicholas C. Gangestad
      3M Company - Senior VP &amp; CFO

================================================================================
Conference Call Participants
================================================================================
   *  Andrew Alec Kaplowitz
      Citigroup Inc, Research Division - MD and U.S. Industrial Sector Head
   *  Andrew Burris Obin
      BofA Merrill Lynch, Research Division - MD
   *  Charles Stephen Tusa
      JP Morgan Chase &amp; Co, Research Division - MD
   *  Deane Michael Dray
      RBC Capital Markets, LLC, Research Division - Analyst
   *  John Fred Walsh
      Crédit Suisse AG, Research Division - Director
   *  John George Inch
      Gordon Haskett Research Advisors - MD &amp; Senior Analyst of Multi-Industrials
   *  Joseph Alfred Ritchie
      Goldman Sachs Group Inc., Research Division - VP &amp; Lead Multi-Industry Analyst
   *  Joshua Charles Pokrzywinski
      Morgan Stanley, Research Division - Equity Analyst
   *  Julian C.H. Mitchell
      Barclays Bank PLC, Research Division - Research Analyst
   *  Laurence Alexander
      Jefferies LLC, Research Division - VP &amp; Equity Research Analyst
   *  Nigel Edward Coe
      Wolfe Research, LLC - MD &amp; Senior Research Analyst
   *  Scott Reed Davis
      Melius Research LLC - Founding Partner, Chairman, CEO &amp; Research Analyst of Multi-Industry Research

================================================================================
Presentation
--------------------------------------------------------------------------------
Operator   [1]
--------------------------------------------------------------------------------
  Ladies and gentlemen, thank you for standing by. Welcome to the 3M First Quarter Earnings Conference Call. (Operator Instructions) As a reminder, this conference is being recorded, Thursday, April 25, 2019.
 I would now like to turn the call over to Bruce Jermeland, Vice President of Investor Relations at 3M.
--------------------------------------------------------------------------------
 Bruce Jermeland,  3M Company - Director of IR   [2]
--------------------------------------------------------------------------------
  Thank you, and good morning, everyone. Welcome to our first quarter 2019 business review. With me today are Mike Roman, 3M's Chief Executive Officer; and Nick Gangestad, our Chief Financial Officer. Mike and Nick will make some formal comments, and then we'll take your questions.
 Please note that today's earnings release and slide presentation accompanying this call are posted on our Investor Relations website at 3m.com under the heading, Quarterly Earnings. Please turn to Slide 2.
 Before we begin, let me remind you to mark your calendars for our upcoming earnings calls on July 25 and October 24. Please take a moment to read the forward-looking statement on Slide 3.
 During today's conference call, we will make certain predictive statements that reflect our current views about 3M's future performance and financial results. These statements are based on certain assumptions and expectations of future events that are subject to risks and uncertainties. Item 1A of our most recent Form 10-K lists some of the most important risk factors that could cause actual results to differ from our predictions.
 Finally, please note that throughout today's presentation, we will be making references to certain non-GAAP financial measures, in particular, measures which exclude the impact of the Tax Cuts and Jobs Act and significant litigation-related charges. Reconciliations of the non-GAAP measures can be found in the appendix of today's presentation and press release.
 Please turn to Slide 4, and I'll hand it off to Mike. Mike?
--------------------------------------------------------------------------------
Operator   [87]
--------------------------------------------------------------------------------
  Ladies and gentlemen, that does conclude the conference call for today. We thank you for your participation and ask that you please disconnect your lines.`;
var m;
var data = [];
var regex = /^[ \t]*(\w+(?:[ \t]+[\w,-]+)*)[\t ]+\[\d+\]\r?\n-+\s+((?:(?:\r?\n)?(?!-).*)*)/mg;

while ((m = regex.exec(str)) !== null) {
  // This is necessary to avoid infinite loops with zero-width matches
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }

  data.push({
    "name": m[1],
    "comment": m[2]
  });
}
console.log(data);

Upvotes: 1

Turtlefight
Turtlefight

Reputation: 10800

A regex like /-{5,}(.*?)-{5,}(.*?)(?:-{5}|$)/gs will capture both the speaker and the spoken text (it will only break if the spoken text / speaker name contain more then 4 dashes after each other -----)
regex101 demo

Explanation:

  • -{5,} will match the first row of ----s, before the speaker name
  • (.*?) captures the speaker name
  • -{5,} matches the second row of ----s, the separator between speaker <-> spoken text
  • (.*?) captures the spoken text
  • (?:-{5}|$) matches either the end of the file or five dashes -----. (this leaves enough dashes for the first part of this regex to match again for the next speaker)

Example:

function sanitizeName(name) {
  // trim it and remove the " [12]" after the name
  return name.replace(/\s*\[\d+\]\s*$/, '').trim();
}

function parseConversation(text) {
  let regex = /-{5,}(.*?)-{5,}(.*?)(?:-{5}|$)/gs;
  let lines = [];
  let match;
  do {
    match = regex.exec(text);
    if (match) {
      lines.push({
        name: sanitizeName(match[1]),
        comment: match[2].trim()
      });
    }
  } while (match);

  return lines;
}

let text = `Q1 2019 3M Co Earnings Call

ST. PAUL Apr 27, 2019 (Thomson StreetEvents) -- Edited Transcript of 3M Co earnings conference call or presentation Thursday, April 25, 2019 at 1:00:00pm GMT

TEXT version of Transcript

================================================================================
Corporate Participants
================================================================================
   *  Bruce Jermeland
      3M Company - Director of IR
   *  Michael F. Roman
      3M Company - CEO &amp; Director
   *  Nicholas C. Gangestad
      3M Company - Senior VP &amp; CFO

================================================================================
Conference Call Participants
================================================================================
   *  Andrew Alec Kaplowitz
      Citigroup Inc, Research Division - MD and U.S. Industrial Sector Head
   *  Andrew Burris Obin
      BofA Merrill Lynch, Research Division - MD
   *  Charles Stephen Tusa
      JP Morgan Chase &amp; Co, Research Division - MD
   *  Deane Michael Dray
      RBC Capital Markets, LLC, Research Division - Analyst
   *  John Fred Walsh
      Crédit Suisse AG, Research Division - Director
   *  John George Inch
      Gordon Haskett Research Advisors - MD &amp; Senior Analyst of Multi-Industrials
   *  Joseph Alfred Ritchie
      Goldman Sachs Group Inc., Research Division - VP &amp; Lead Multi-Industry Analyst
   *  Joshua Charles Pokrzywinski
      Morgan Stanley, Research Division - Equity Analyst
   *  Julian C.H. Mitchell
      Barclays Bank PLC, Research Division - Research Analyst
   *  Laurence Alexander
      Jefferies LLC, Research Division - VP &amp; Equity Research Analyst
   *  Nigel Edward Coe
      Wolfe Research, LLC - MD &amp; Senior Research Analyst
   *  Scott Reed Davis
      Melius Research LLC - Founding Partner, Chairman, CEO &amp; Research Analyst of Multi-Industry Research

================================================================================
Presentation
--------------------------------------------------------------------------------
Operator   [1]
--------------------------------------------------------------------------------
  Ladies and gentlemen, thank you for standing by. Welcome to the 3M First Quarter Earnings Conference Call. (Operator Instructions) As a reminder, this conference is being recorded, Thursday, April 25, 2019.
 I would now like to turn the call over to Bruce Jermeland, Vice President of Investor Relations at 3M.
--------------------------------------------------------------------------------
 Bruce Jermeland,  3M Company - Director of IR   [2]
--------------------------------------------------------------------------------
  Thank you, and good morning, everyone. Welcome to our first quarter 2019 business review. With me today are Mike Roman, 3M's Chief Executive Officer; and Nick Gangestad, our Chief Financial Officer. Mike and Nick will make some formal comments, and then we'll take your questions.
 Please note that today's earnings release and slide presentation accompanying this call are posted on our Investor Relations website at 3m.com under the heading, Quarterly Earnings. Please turn to Slide 2.
 Before we begin, let me remind you to mark your calendars for our upcoming earnings calls on July 25 and October 24. Please take a moment to read the forward-looking statement on Slide 3.
 During today's conference call, we will make certain predictive statements that reflect our current views about 3M's future performance and financial results. These statements are based on certain assumptions and expectations of future events that are subject to risks and uncertainties. Item 1A of our most recent Form 10-K lists some of the most important risk factors that could cause actual results to differ from our predictions.
 Finally, please note that throughout today's presentation, we will be making references to certain non-GAAP financial measures, in particular, measures which exclude the impact of the Tax Cuts and Jobs Act and significant litigation-related charges. Reconciliations of the non-GAAP measures can be found in the appendix of today's presentation and press release.
 Please turn to Slide 4, and I'll hand it off to Mike. Mike?
--------------------------------------------------------------------------------
Operator   [87]
--------------------------------------------------------------------------------
  Ladies and gentlemen, that does conclude the conference call for today. We thank you for your participation and ask that you please disconnect your lines.`;
  

console.log(parseConversation(text));

Upvotes: 0

Related Questions