Home>

### is it possible to always use any number specified with a regular expression once every time?

For example, if you always use 1, 2, and 3 for a 3-digit number, is it possible to specify a regular expression that gives the following results?

123 true
132 true
213 true
231 true
312 true
321 true
124 false

What you do n’t understand

I don't know if it's possible or impossible with regular expressions.

Regular expression checker
I tried a lot on this site, but I couldn't get the results I wanted with regular expressions.

It can be implemented using prefetching.

``/^ (? =. * 1) (? =. * 2) (? =. * 3). {3} \$/``

The`(? =. * 1)`in the first half means "1 will come somewhere" (the same applies to 2 stores 3).

Although it is possible with Maisumakun's answer, I don't recommend it because regular expressions and AND conditions are inefficient.

`[\ s \ S] *`or modifier`s`

if a non-numeric character contains a line break

Is exact match or partial match

There are many things to think about.
If it is a simple partial match AND condition, I think there are other suitable functions.

Re: opyon-san

The comparison with and without regular expressions is provided for your reference.

"^ (? =. * 1) (? =. * 2) (? =. * 3) (? =. * 4) (? =. * 5) (? =. * 6) (? =. * 7) (? =. * 8) (? =. * 9). {9} \$"
123456789 true
223456789 false

Regular expression: Regular expression checker

Reference issues
Original: Pandigital multiples Problem 38
Translation: Problem 38 "Pan Digital Multiple"

``````import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class pe_10038 {
public static void main (String [] args) {
long start = System.nanoTime ();
//example
// 192 (1,2,3)
// 192384576
func (9999);
// no regular expression
// 9327
// 2
// 932718654
// 1.749699ms
long end = System.nanoTime ();
System.out.println ((end-start)/1000000f + "ms");
System.out.println ();
start = System.nanoTime ();
func2 (9999);
// regular expression
// 9327
// 2
// 932718654
// 6.028767ms
end = System.nanoTime ();
System.out.println ((end-start)/1000000f + "ms");
}
static void func2 (int n) {
int max = 0;
// With regular expression
// for regular expressions
Pattern p1 = Pattern.compile (
"^ (? =. * 1) (? =. * 2) (? =. * 3) (? =. * 4) (? =. * 5)"
+ "(? =. * 6) (? =. * 7) (? =. * 8) (? =. * 9). {9} \$");
Matcher m1;
for (int i = n;i>0;i--) {
String s = "";
for (int j = 1;j<= 9;j ++) {
s + = i * j;
int len ​​= s.length ();
if (len>9) {
break;
} else if (len<9) {
continue;
}
// With regular expression
m1 = p1.matcher (s);
if (m1.find ()) {
System.out.println (i);
System.out.println (j);
// System.out.println (s);
max = Math.max (max, Integer.parseInt (s));
}
}
if (max! = 0) {
System.out.println (max);
break;
}
}
}
static void func (int n) {
int max = 0;
// Without regular expression
for (int i = n;i>0;i--) {
String s = "";
for (int j = 1;j<= 9;j ++) {
s + = i * j;
int len ​​= s.length ();
if (len>9) {
break;
} else if (len<9) {
continue;
}
// Without regular expression
if (chk (s)) {
System.out.println (i);
System.out.println (j);
// System.out.println (s);
max = Math.max (max, Integer.parseInt (s));
}
}
if (max! = 0) {
System.out.println (max);
break;
}
}
}
private static boolean chk (String s) {
int [] chk = new int ;
for (int i = 1;i<= 9;i ++) {
chk [(s.charAt (i-1)-'0')] ++;
}
for (int i = 1;i<= 9;i ++) {
if (chk [i]! = 1) {
return false;
}
}
return true;
}
}``````