locked
Regex to allow a list of numbers separated by | RRS feed

  • Question

  • User657329123 posted

    Hi there,

    I have a regular expression that allow integers and/or decimal /^((\s*)|([0-9]\d{0,9}(\.\d{1,3})?%?$))$/. How do I allow users to enter 0 to 5 groups of integers and/or decimals separated by |.

    Joe

    Monday, September 17, 2018 3:02 PM

Answers

  • User839733648 posted

    Hi  joegreen2005,

    “ How do I allow users to enter 0 to 5 groups of integers and/or decimals separated by |”

    When you want to match the  special  character |, you have to add \ in front of it.

    And you just could use {n,m} to define the times you want your string appear.

    You could try:

    ^((\s*)|([0-9]\d{0,9}(\.\d{1,3})?%?))*(\|((\s*)|([0-9]\d{0,9}(\.\d{1,3})?%?))+){0,4}$

    And you may see the match result from the image:

    For more details about Regex, you may refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-asterisk 

    The online  regex testing you could refer to: https://regex101.com/ 

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 18, 2018 10:16 AM

All replies

  • User839733648 posted

    Hi  joegreen2005,

    “ How do I allow users to enter 0 to 5 groups of integers and/or decimals separated by |”

    When you want to match the  special  character |, you have to add \ in front of it.

    And you just could use {n,m} to define the times you want your string appear.

    You could try:

    ^((\s*)|([0-9]\d{0,9}(\.\d{1,3})?%?))*(\|((\s*)|([0-9]\d{0,9}(\.\d{1,3})?%?))+){0,4}$

    And you may see the match result from the image:

    For more details about Regex, you may refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-asterisk 

    The online  regex testing you could refer to: https://regex101.com/ 

    Best Regards,

    Jenifer

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 18, 2018 10:16 AM
  • User657329123 posted

    Is this expression correct to allow string with or without spaces seperated by |.

    ^((\s*)|([a-zA-Z ]?%?))*(\|((\s*)|([a-zA-Z ]?%?))+){0,4}$

    I tested it and it seems OK but need validation.

    Thursday, September 20, 2018 6:07 PM