web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

Need help with Regular expression

(5) ShareShare
ReportReport
Posted on by 37
I need assistance with regular expression.  I am using this regular expression "^[a-zA-Z0-9]+-\\d" on field using X++ where I want to accept only alphanumeric characters. But only exception that last two characters are - and a number. @Martin Dráb Thank you.
I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at
    Assuming that you're using System.Text.RegularExpressions.Regex, your regex looks good to me, with one exception. You only check that the string begins with this pattern; any characters are allowed to follow. For example, aB1-8*^* is valid, because aB1-8 matches the pattern and the pattern says nothing about subsequent characters.
     
    If you want want a pattern where the number is the last character, add $ at the end. Therefore the pattern can be written as '^[a-zA-Z0-9]+-\\d$' or @'^[a-zA-Z0-9]+-\d$'.
  • subodhkant Profile Picture
    37 on at
    Thanks for the quick response. @Martin Drab
     
    But it is not accepting the input "VQVI005293-1". Also, it is accepting all special characters second last position rather it should accept only dash.
  • Verified answer
    André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at
    Hi Subodhkant,
     
    Martin is correct with his suggestion. The example you provided is working for me. You can learn more about regular expressions and perform tests using this site: RegExr: Learn, Build, & Test RegEx
     
    In addition, you can use Copilot chat and ask for a regular expression which has in my experience amazing results.
     
     
  • subodhkant Profile Picture
    37 on at
    Hi Andre,
    form field is not accepting it.
     
    RegEx is accepting it though 
  • Suggested answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at
    The problem is that you show an error and return false if the check succeeds and IsMatch() returns true. You seem to mean if (!regex.IsMatch(value)).
  • Suggested answer
    subodhkant Profile Picture
    37 on at
    Thanks Martin and Andre. It worked for me.
     
  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at
    Just note that your pattern doesn't ensure that the string ends with a dash and a number. For example, banana matches your pattern.
     
    By the way, there are online tools such as Regex Tester that you can use to quickly validate your regular expressions.
  • subodhkant Profile Picture
    37 on at
    Sorry Martin, Maybe I did not put the requirement correctly.
     
    String is acceptable as alphanumeric only; only exception is last two characters can or can-not be dash and a number.
     
    My apologies for the confusion. Thank you for your valuable inputs and time.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Siv Sagar Profile Picture

Siv Sagar 283 Super User 2025 Season 2

#2
Laurens vd Tang Profile Picture

Laurens vd Tang 213 Super User 2025 Season 2

#3
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 152 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans