Home > OS >  How to get the match for the following use cases from this regex pattern?
How to get the match for the following use cases from this regex pattern?

Time:02-21

I have the Regex to match the following patterns,

Link to the Use case: https://regex101.com/r/wnp1k4/1

How can i get the match for the same by modifying the Regex? please help.

(?:^|(?<=[\D;a-zA-Z(),.:;?!"'`>]))(?!000|666|9)(?<![Oo][Rr][Dd][Ee][Rr].)(?<![Oo][Rr][Dd][Ee][Rr]..)(?<![Oo][Rr][Dd][Ee][Rr]...)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr].)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr]..)(?<![Oo][Rr][Dd][Ee][Rr].[Nn][Uu][Mm][Bb][Ee][Rr]...)(?<![Xx])\d{3}[ -.=\n\r]{0,10}(?!00)\d{2}[ -.=\n\r]{0,10}(?!0000)\d{4}(?:$|(?=[\Da-zA-Z(),.:;?!"'`<= ]))

Order numbers should not get detected if 'X or x' precedes the number. so this is working fine.

x123456789

X123456789

x123-456-789

X123-456-789

123-456-789

Need to modify the regex pattern to get the match for the list of ordernumbers written like below...along with the word (order number) should be case insensitive.

ordernumber123-456-789

order number123-456789

order number 123456789

123-456789

123456789

ordernumber-123456787

ordernumber - 123456789

ordernumber #123456789

ordernumber anysplcharacter123456789

CodePudding user response:

Converting my comment to answer so that solution is easy to find for future visitors.

You may use this regex:

(?<!\d)(?!000|666|9)(order\W?number)?\W*(?<!x)\d{3}[ .=-]{0,10}(?!000)\d{3}[ .=-]{0,10}(?!000)\d{3}

RegEx Demo

RegEx Details:

  • (?<!\d): Make sure that previous character is not a digit
  • (?!000|666|9): Make sure that we don't have 000 or 666 or 9 at the next position
  • (order\W?number)?: Match order and number optionally separated with a non-word character
  • \W*: Match 0 or more non-word characters
  • (?<!x): Make sure previous character is not x
  • \d{3}: Match 3 digits
  • [ .=-]{0,10}: Match 0 to 10 instances of given separators
  • (?!000): Make sure we don't have 000 at next position
  • \d{3}: Match 3 digits
  • [ .=-]{0,10}: Match 0 to 10 instances of given separators
  • (?!000): Make sure we don't have 000 at next position
  • \d{3}: Match 3 digits

CodePudding user response:

/((?<key>order\s*number).*)?(?<!x)(?<value>\d{9}|\d{3}-\d{6}|\d{3}-\d{3}-\d{3})/gim

const multilineSample = `x123456789
X123456789
x123-456-789
X123-456-789

123-456-789

ordernumber123-456-789
order number123-456789
order number 123456789

123-456789
123456789

ordernumber-x123456787

ordernumber-123456787
ordernumber - 123456789
ordernumber #123456789
ordernumber *anysplcharacter*123456789
ordernumber !@#$%^&123456789

ordernumber !@#$%^&x123456789`;

const regXOrderNumber =
  // see ... [https://regex101.com/r/wnp1k4/6]
  /((?<key>order\s*number).*)?(?<!x)(?<value>\d{9}|\d{3}-\d{6}|\d{3}-\d{3}-\d{3})/gim;

console.log(
  'all results each with its mapped named capture group ...',
  Array.from(
    multilineSample.matchAll(regXOrderNumber)
  )
  .map(({ groups }) => groups)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

try this.

^([^x]|(order\s?number\s?)(\s?[-|#]?\s?))(anysplcharacter)?([\d-] )$

Is there another case?

  • Related