所以问题是我有两个字符串。一个字符串是随机分类的字母。第二个是+、*、$或{N}。其中+号表示所有26个字母,$表示数字1-9,星号表示任意3个重复字符,除非{N}以不同方式显示。
示例:输入:"+++++*“"abcdehhhhhh”输出: false
输入:"$**+*{2}“”9mmmrrkbb“输出: true
试题我答错了,试着理解答案。
发布于 2020-02-29 04:02:27
我会通过迭代模式,并保持一个指针,指向我们现在正在检查的字符串中的位置。这样,您可以将指针或多或少地移动{N}中的N(默认为3);在每种情况下,如果字符不在允许的集合中,我只返回false,对于*,我计算出当前字符应该重复多少次,并检查下一个N字符是否等于input[x].repeat(N) (当前字符重复N次)。
使用regex也可以做到这一点,但老实说,我觉得由于需要将每个字符转换为一个类或重复匹配,它最终会变得更加复杂。
function matchPattern(input, pattern) {
let x = 0; //pointer to input string
for(let i = 0; i < pattern.length; i++) {
switch(pattern[i]) {
case '+':
if(!'abcdefghijklmnopqrstuvwxyz'.includes(input[x++])) return false;
break;
case '$':
if(!'0123456789'.includes(input[x++])) return false;
break;
case '*':
let repeatNum = 3;
if(pattern[i+1] == '{' && pattern[i+3] == '}') repeatNum = +pattern[i+2];
if(input[x].repeat(repeatNum) != input.slice(x,x+=repeatNum)) return false;
break;
}
}
return x >= input.length;
}
console.log(matchPattern("abcdehhhhhh", "+++++*"));
console.log(matchPattern("9mmmrrrkbb", "$**+*{2}"));
发布于 2020-02-29 05:06:28
我们可以使用String.prototype.replace()和RegExp构造函数将模式字符串转换为正则表达式,然后使用RegExp.prototype.test()与输入字符串进行比较:
function re(pattern) {
let capture = 1;
const source = pattern.replace(
/([+*$])(?:\{(\d+)\})?/g,
(match, rule, repeat = 3) => {
switch (rule) {
case '+': return '[a-z]';
case '*': return `(.)\\${capture++}{${repeat - 1}}`;
case '$': return '\\d';
}
}
);
// the generated regular expression
console.log(source);
return new RegExp(`^${source}$`);
}
function compare(input, pattern) {
const result = re(pattern).test(input);
console.log(`'${input}' matches '${pattern}': ${result}`);
return result;
}
compare('abcdehhhhhh', '+++++*');
compare('9mmmrrrkbb', '$**+*{2}');
请注意,生成的正则表达式可能很长且重复,因此我们也可以使用quantifiers在正则表达式的源代码中编码重复规则:
function re(pattern) {
let capture = 1;
const source = pattern.replace(
/([+*$])(?:\{(\d+)\})?/g,
(match, rule, repeat = 3) => {
switch (rule) {
case '+': return '[a-z]';
case '*': return `(.)\\${capture++}{${repeat - 1}}`;
case '$': return '\\d';
}
}
).replace(
/(.+?)\1+/g,
(match, capture) => {
const rle = `(?:${capture}){${match.length / capture.length}}`;
// only replace it if the rle is actually shorter
return rle.length < match.length ? rle : match;
}
);
// the generated regular expression
console.log(source);
return new RegExp(`^${source}$`);
}
function compare(input, pattern) {
const result = re(pattern).test(input);
console.log(`'${input}' matches '${pattern}': ${result}`);
return result;
}
compare('abcdehhhhhh', '+++++*');
compare('9mmmrrrkbb', '$**+*{2}');
发布于 2021-10-13 07:44:07
下面的代码给出了您的答案,尽管它是上面代码的修改
function StringChallenge(str) {
// code goes here
let wordsArr = str.split(" ");
function re(pattern) {
let capture = 1;
const source = pattern
.replace(/([+*$])(?:\{(\d+)\})?/g, (match, rule, repeat = 3) => {
switch (rule) {
case "+":
return "[a-z]";
case "*":
return `(.)\\${capture++}{${repeat - 1}}`;
case "$":
return "\\d";
}
})
.replace(/(.+?)\1+/g, (match, capture) => {
const rle = `(?:${capture}){${match.length / capture.length}}`;
return rle.length < match.length ? rle : match;
});
return new RegExp(`^${source}$`);
}
function compare(pattern, input) {
const result = re(pattern).test(input);
return result;
}
return compare(wordsArr[0], wordsArr[1]);
}
console.log(StringChallenge("$**+*{2} 9mmmrrrkbb"));
console.log(StringChallenge("$**{2} ammmrrbb"));https://stackoverflow.com/questions/60458111
复制相似问题