skip to Main Content

I have a channel in slack where automated message is posted. The message looks like this:

A ticket is submitted by: @abc

Request Title test

Requested For @xyz

Requested system? MMM

See here for the ticket.

What i need to do is to fetch the system name ‘MMM’ or there can be multiple system name separated by comma.

I am trying the following appscript code but this is not fetching anything:

function extractData(message) {
  const regexRequestedSystem = /Requested system?n(.*?)$/s;
  const requestedSystemMatch = regexRequestedSystem.exec(message);

  return {
    requestedSystem: requestedSystemMatch ? requestedSystemMatch[1].trim() : null,
    messageLink: `https://slack.com/app_redirect?channel=${channelId}&message_ts=${message.ts}`,
  };
}

Could you please help me with the regex syntax?

Thank you!!

I tried this syntax:

const regexRequestedSystem = /Requested system?n(.*?)$/s;

but this is not fetching anything. I need to fetch the ‘MMM’ only like I said before.

2

Answers


  1. Here you can get Requested system Names in an array

    const text = `
    A ticket is submitted by: @abc
    
    Request Title test
    
    Requested For @xyz
    
    Requested system? MMM, LLL, KKK
    
    See here for the ticket.
    `;
    
    const regex = /Requested system?s*([ws,]+)$/m; // Match until the end of the line
    const match = text.match(regex);
    
    if (match) {
        const systems = match[1].split(',').map(name => name.trim()); // Splits by commas and trims spaces
        console.log(systems); // Outputs: ['MMM', 'LLL', 'KKK']
    } else {
        console.log("No match found");
    }
    Login or Signup to reply.
  2. Try this approach

    I used Google Docs to give as a sample for your question and here’s some modification on your code.

    Regex const regex = /Requested system?s*(.+)/;

    This part is a capturing group () that matches one or more characters (.+) after the spaces.
    This will capture everything that follows "Requested system?" in the document, which could be the list of systems or any other content.

    Modified Script

    function extractRequestedSystem() {
      const doc = DocumentApp.getActiveDocument();
      const bodyText = doc.getBody().getText();
      const regex = /Requested system?s*(.+)/;
      const match = bodyText.match(regex);
      
      if (match) {
        const requestedSystem = match[1].trim();
        Logger.log(`Requested System: ${requestedSystem}`);
      } else {
        Logger.log('No match found');
      }
    }
    

    Sample Validation

    sample1

    sample2

    Reference

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search