-3

I am trying to find a regex which supports a pattern like below:

  1. String starts with //
  2. String has a delimiter after //; (suppose ; is the delimiter)
  3. String has \n after demiliter (//;\n)
  4. Finally String contains any number of digits with that delimiter (//;\n2;3;4;5)

Could you help?

I tried ^//\\D+\\n.*$ but it doesn't work.

Thanks in advance!

JvdV
  • 53,146
  • 6
  • 36
  • 60
Kush Sharma
  • 39
  • 1
  • 8
  • 3
    java or javascript? have you made any attempt of this yourself? – depperm Mar 02 '21 at 16:53
  • I am trying in Java. I tried ^//\\D+\\n.*$ but it doesn't work. Yeah delimiter can change. Even if we get to match pattern till \n then also my work is done! – Kush Sharma Mar 02 '21 at 17:01

2 Answers2

-1

Sample: //;\n2;3;4;5

Answer: [/]{2}[;]\\[n](\d[;]){1,999}\d

This will allow further combinations of a decimal followed by ; \d is added at the end in the case a semicolon is not added judging by your sample

RavinderSingh13
  • 117,272
  • 11
  • 49
  • 86
Nate
  • 15
  • 4
-1

Okay based on your additional comment this could work. It's very messy but it may just get the job done.

var string = "//;\n2;3;4;5";

console.log(
  
  string.replace(/[^0-9,.]+/g," ").trim().split(" ").map(function(x){return parseInt(x, 10);}).reduce(function(a, b){return a + b;}, 0)

);

Console log results in 14

Jacob Riches
  • 137
  • 8
  • Basically there is a string which starts with // followed by a delimiter and then in new line, there are any number of numbers seperated by that delimiter. I have to add these numbers. Hence, I need a regex to first find the string is valid and then retrieve the numbers, – Kush Sharma Mar 02 '21 at 17:27