如何检查是否可以使用正则表达式来旋转文本? [英] How to check if the text can be spinned using regex?
问题描述
我正在尝试使用正则表达式旋转文本.我正在尝试使其适用于Googles Apps脚本.我已经尝试过表达式(\{[^\}]+\}|[^\{\}]*)
,但是在许多情况下它都失败了.我只想要它
如果您不了解旋转文本的含义,请在下面进行解释:
I am trying to spin text using a regular expression. I am trying to make it work for Googles Apps Script. I have tried the expression (\{[^\}]+\}|[^\{\}]*)
, but it fails in many cases. I just want it to
In case you don't know what I mean by spinning text, it's explained below:
{This|That} is a {cat|dog}
给出
This is a cat
This is a dog
That is a cat
That is a dog
推荐答案
我不会为此使用正则表达式,而只会实现一个状态机.
I would not use a regular expression for this, I would just implement a state machine.
您只需跟踪自己所处的级别.这是基本步骤,可以根据需要扩展为递归工作.
You just keep track of what level you're on. This is rudimentary, and can be expanded to work recursively if needed.
- https://cs.stackexchange.com/questions/11044 /two-state-turing-machine-for括号匹配
- http://raganwald.com/2019 /02/14/i-love-programming-and-programmers.html
- https://cs.stackexchange.com/questions/11044/two-state-turing-machine-for-parenthesis-matching
- http://raganwald.com/2019/02/14/i-love-programming-and-programmers.html
/** Can also be written as:
*
* const isValid=s=>s.split('').reduce((r,c)=>c==='{'?r+1:(c==='}'?r-1:r),0)===0;
*/
function isValid(str) {
var state = 0;
for (let i = 0; i < str.length; i++) {
switch (str.charAt(i)) {
case '{': state++; break;
case '}': state--; break;
}
}
return state === 0;
};
.as-console-wrapper { top: 0; max-height: 100% !important; }
更好的方法是由内而外地工作.只需获取一个开括号的LAST索引,并检查它是否在它之后但在下一个开括号之前有一个闭括号(和管道)即可.
A better approach would be to work inside-out. Just get the LAST index of an opening brace, and check to see if it has a closing brace (and pipe) after it, but before the next opening brace.
function isValid(str, strict) {
var maxAttempts = 100; // Do not let this loop too many times...
while (str.lastIndexOf('{') < str.indexOf('}', str.lastIndexOf('{'))) {
let start = str.lastIndexOf('{');
let nextStart = str.indexOf('{', start + 1); // Check ahead (optional)
let mid = str.indexOf('|', start); // If you need a pipe check...
let end = str.indexOf('}', start);
//console.log(JSON.stringify({ str : str, start : start, mid : mid, end : end}));
let isValid = start > -1 && end > -1 && (nextStart === -1 || nextStart > end);
if (strict === true && (mid < start || mid > end)) {
return false; // If pipes are required, check for their existence.
}
if (isValid) {
str = str.substring(0, start) + str.substr(end + 1);
}
// Safeguard for accidental infinite recursion...
if (maxAttempts-- < 0) { throw Error('Inifinite recursion detected!'); }
}
return str.indexOf('{') === -1 || str.indexOf('}') === -1;
}
/* Valid */ console.log(isValid('{|}'));
/* Valid */ console.log(isValid('{|}', true));
/* Invalid */ console.log(isValid('{}', true));
/* Invalid */ console.log(isValid('}{'));
/* Invalid */ console.log(isValid('{}}{{}'));
.as-console-wrapper { top: 0; max-height: 100% !important; }
这篇关于如何检查是否可以使用正则表达式来旋转文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!