Skip to main content
bebarth
Braniac
May 13, 2023
Answered

Regular expression to find 2 digits in a string

  • May 13, 2023
  • 1 reply
  • 1613 views

Hi,

I am looking for a regexp to for checking a string is containing both the #1 and #3 digits but numbers including these digits are not take in account (12, 368, 51, 6983, etc.);

I couldn't write the expression all at once, so I compiled the search for the digit 1 and the search for the digit 3.

Here is a quick explanation, for finding the digit 1, the regular expression I wrote is:

/(.*[^0-9]1[^0-9].*)/

But that doesn’t work if “1” is at the beginning or at the end of the string, so for testing I add a “x” character at these both places.

And the same for the digit “3”.

Attached is an example of the complete script I wrote.

if (event.value=="") this.getField("light").fillColor=color.transparent;
else if (/(.*[^0-9]1[^0-9].*)/.test("x"+event.value+"x") && /(.*[^0-9]3[^0-9].*)/.test("x"+event.value+"x")) this.getField("light").fillColor=color.green;
else this.getField("light").fillColor=color.red;

I wonder if it's possible to write a single expression for that without adding both characters for testing.

Thanks in advance if somebody has an idea...

@+

This topic has been closed for replies.
Correct answer Nesa Nurani

Try this:

/(?=.*(^|\D)1(\D|$))(?=.*(^|\D)3(\D|$))/

1 reply

Nesa Nurani
Nesa NuraniCorrect answer
Inspiring
May 13, 2023

Try this:

/(?=.*(^|\D)1(\D|$))(?=.*(^|\D)3(\D|$))/

bebarth
bebarthAuthor
Braniac
May 13, 2023

Great, thanks!

That seems so obvious when we have the solution in front of the eyes...

@+