0

I am trying to get lines from a string parameter and copy them in a new string variable. This logic will execute as long as the new line doesn't match a particular regex expression.

From some reason (unknown to me) the output it's what I've expected....

This is the code:

matchRegexExp(log: string) {

            let regexString = /(?:\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})[\s\S]+?((?=\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})|$)/g;

            return log.match(regexString);
        }
}

private createString(string1: string) {

        let i: number = 0;
        let readLine: string[] = string1.split('\n');
        let x: string ='';

        while (this.matchRegexExp(readLine[i]) == null) {

            console.log('regex expression returns... ' + this.matchRegexExp(readLine[i]));
            console.log('current line content is... ', readLine[i]);
            console.log('x is = ', x);

            x = x + readLine[i];

            console.log('new x is ',x , '\n');
            i++;
        }
        console.log('final x is = ', x, '\n');

        return x;
    }

This is the data from string1:

ana
has
apples
and 
  oranges
2019-01-01 11:11:11 INFO ThisApp - Started App

The lines that do not match my regex expression and that have to be copied in the string are:

ana
has
apples
and 
  oranges

But when I run the code... I get this 'weird' output:

regex expression returns... null
current line content is...  ana
x is =
 ew x is  ana

regex expression returns... null
current line content is...  has
x is =  ana
 as x is  ana

regex expression returns... null
current line content is...  apples
hass =  ana
 pplesis  ana

regex expression returns... null
current line content is...  and
apples  ana
 nd esis  ana

regex expression returns... null
current line content is...    oranges
and es  ana
  oranges ana

  orangess =  ana
6
  • What is this.matchRegexExp? Commented Jul 4, 2019 at 12:24
  • What's the environment you run it in? The code seems correct basically, see typescriptlang.org/play/index.html#code/… Commented Jul 4, 2019 at 12:31
  • @adiga I've updated the code with the 'matchRegexExp' method Commented Jul 4, 2019 at 12:33
  • @TPReal The code it's written in typescript and run with node v.10.15.3 in VS Code Commented Jul 4, 2019 at 12:42
  • It'd be great to have a fully runnable version of this code.let regexString = *some regex expression*; isn't valid js and the braces don't actually match Commented Jul 4, 2019 at 12:56

2 Answers 2

1

It looks to me like a CRLF problem. You're splitting the input string on '\n'. However, if the input string has line separators '\r\n' (as data that has come from Windows is likely to), then you'll end up with x containing something like:

ana\rhas\rapples\r\rand \roranges

which when printed out will look very odd ('\r' will reset the cursor to the start of the line).

Try splitting the input string on '\r\n' and see if that helps.

Alternatively, when you build up 'x', you could add the '\n' back again to produce a multi-line string:

x = x + readLine[i] + '\n';
Sign up to request clarification or add additional context in comments.

Comments

0

I think your printing is wrong. You need console.log('new x is ' + x + '\n). Please check console.log documentation.

I have tried your code - a little bit modified version (using simple node JS project):

function matchRegexExp(log) {
  let regexString = /(?:\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})[\s\S]+?((?=\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})|$)/g;

  return log.match(regexString);
}

function createString(string1) {
  console.log("Value of string1 is:");
  console.log(string1);
  console.log()

  let i = 0;
  let readLine = string1.split('\n');
  let x ='';

  while (i < readLine.length) {
    if (matchRegexExp(readLine[i]) === null) {
      console.log('regex expression returns... ' + matchRegexExp(readLine[i]));
      console.log('current line content is... ', readLine[i]);
      console.log('x is = ' + x);

      x = x + readLine[i];

      console.log('new x is ' + x + '\n');
    }
    i++;
  }
  console.log('final x is = '+  x + '\n');

  return x;
}


const testString = `ana
has
apples
and 
  oranges
2019-01-01 11:11:11 INFO ThisApp - Started App`;

createString(testString);

I get this printout:

Value of string1 is:
ana
has
apples
and 
  oranges
2019-01-01 11:11:11 INFO ThisApp - Started App

regex expression returns... null
current line content is...  ana
x is = 
new x is ana

regex expression returns... null
current line content is...  has
x is = ana
new x is anahas

regex expression returns... null
current line content is...  apples
x is = anahas
new x is anahasapples

regex expression returns... null
current line content is...  and 
x is = anahasapples
new x is anahasapplesand 

regex expression returns... null
current line content is...    oranges
x is = anahasapplesand 
new x is anahasapplesand   oranges

final x is = anahasapplesand   oranges

I'd like to note that I'm using a CRLF file.

Is this the result you were going for?

1 Comment

Same result... Tried it your way at first

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.