1

sample data is like

@ID_111_I1_0.000_I2_0.00_I3_-0.999*
@ID_232_TS_09.0*

I created one regular expression like :

const reg = new RegExp(/^@ID_[A-Z0-9._-]*\*/i);

but this will show valid true for strings like @ID_111_I1_0.000_I2*

here I2 key is there, but its value is not showing string start with this @ID . then split with _ key value pair and string end with * Please help me with a solution !!!!

8
  • 1
    stackoverflow.com/editing-help Commented Feb 9, 2021 at 7:22
  • Please explain what you are trying to do with this regular expression. What is your desired output? Commented Feb 9, 2021 at 7:24
  • What is the problem in matching @ID_111_I1_0.000_I2* ? Commented Feb 9, 2021 at 7:26
  • 1
    is this of help: regex101.com/r/n3CJmI/1 ? Commented Feb 9, 2021 at 7:30
  • 2
    You may use /^@ID_[A-Z0-9.-]+(?:_[A-Z0-9]+_[A-Z0-9.-]+)*\*$/ Commented Feb 9, 2021 at 8:07

1 Answer 1

3

Check if the following works for you:

^@ID_\d+(?:_[A-Z\d]+_-?\d+(?:\.\d+)?)+\*$

See the online demo

  • ^ -Start string anchor.
  • @ID_ - Match literally what is mentioned.
  • \d+ - 1+ numbers 0-9 to get the ID#.
  • (?: - Open 1st non-capture group:
    • _[A-Z\d]+ - An underscore followed by 1+ alphanumeric characters.
    • _-?\d+ - An underscore, optional hyphen and 1+ digits.
    • (?: - Open 2nd non-capture group:
      • \.\d+ - A literal dot and 1+ numbers.
      • )? - Close 2nd non-capture group and make it optional.
    • )+ - Close 1st non-capture group and match it 1+ times.
  • \* - Literal asterisk.
  • $ - End string anchor.

Note that I made it optional to have decimals in the key-value pairs. You could make it obligatory if need be:

^@ID_\d+(?:_[A-Z\d]+_-?\d+\.\d+)+\*$
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely explained

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.