英文:
Regex for calculator display
问题
以下是翻译后的内容,只包括代码部分:
我想知道是否可以为具有以下条件的计算器输入创建正则表达式,显示从```0```开始:
- 第一个字符可以是```+```或```-```符号(可选)
- 如果第一个输入是```.```,它应附加到```0```后面
- 如果第一个输入是数字,则应占据```0```的位置
- 数字只能有一个```.```。
- 在数字之后,可以有运算符```+-/*```。
- 运算符```/*```可以后跟```+-```。
- 如果输入是运算符,并且最后一个字符是运算符,则它应占据最后一个字符的位置
允许的输入示例:
``` -0.548*-7+54 ```
错误的输入示例:
```*354*/56-+45```
我可以通过```regex```和```条件语句```的混合来实现这一点(我正在使用React):
```javascript
const displayPattern = /([+-]?)(\d+)(\.?)(\d*)([*/]?)([+-]?)/g;
const isOperator = /[-+*/]/;
const isNumber = /\d/;
const isSign = /[-+]/;
const lastChar = state.slice(-1);
if (value.includes("AC")) {
setState("0");
} else if (state.length === 1) {
if (isNumber.test(value)) {
if (state === "0") {
setState(value);
} else {
setState(prevVal => prevVal + value);
}
} else if (value === ".") {
setState(prevVal => prevVal + value);
} else if (isSign.test(value)) {
setState(value);
}
} else {
if (isOperator.test(lastChar) && isOperator.test(value)) {
if (isSign.test(lastChar)) {
setState(prevVal => prevVal.slice(0, -1) + value);
} else {
!isSign.test(value) && setState(prevVal => prevVal.slice(0, -1) + value);
}
}
setState(
prevVal =>
(prevVal + value).match(displayPattern) &&
(prevVal + value).match(displayPattern).join("")
);
}
<details>
<summary>英文:</summary>
I was wondering if it is possible to make a regex for a calculator input with these conditions, the display starts with a ```0```:
- the fist char can be an ```+``` or ```-``` sign (optional)
- if the first input is a ```.``` it should be appended to the ```0```
- if the first input is a number it should take the place of the ```0```
- the number can have only one ```.```
- after a number I can have an operator ```+-/*```
- the operator ```/*``` can be followed by an ```+-```
- if the input is an operator and the last char is a operator it should take the place of the last char
Allowed input example:
``` -0.548*-7+54 ```
Wrong input example:
```*354*/56-+45```
I could do that with a mix of ```regex``` and ```conditionals``` (I'm using React):
const displayPattern = /([+-]?)(\d+)(.?)(\d*)([/]?)([+-]?)/g;
const isOperator = /[-+/]/;
const isNumber = /\d/;
const isSign = /[-+]/;
const lastChar = state.slice(-1);
if (value.includes("AC")) {
setState("0");
} else if (state.length === 1) {
if (isNumber.test(value)) {
if (state === "0") {
setState(value);
} else {
setState(preVal => preVal + value);
}
} else if (value === ".") {
setState(preVal => preVal + value);
} else if (isSign.test(value)) {
setState(value);
}
} else {
if (isOperator.test(lastChar) && isOperator.test(value)) {
if (isSign.test(lastChar)) {
setState(prevVal => prevVal.slice(0, -1) + value);
} else {
!isSign.test(value) && setState(prevVal => prevVal.slice(0, -1) + value);
}
}
setState(
preVal =>
(preVal + value).match(displayPattern) &&
(preVal + value).match(displayPattern).join("")
);
}
</details>
# 答案1
**得分**: 2
以下是翻译好的部分:
像这样的代码可以工作:
^[+-]?\d*\.*\d+(?:[+*\/-][+-]?\d*\.*\d+)*$
- `^` - 字符串开始锚点
- `[+-]?` - 可选择以正负号开头
- `\d*\.*\d+` - 可选择数字,后跟可选择小数,后跟必须数字。这允许我们考虑整数或小数。
- `(?:[+*\/-][+-]?\d*\.*\d+)*` - 以必须的前置运算符重复可选择的前两个项目的模式
- `$` - 字符串结束锚点
https://regex101.com/r/srl7vj/1
<details>
<summary>英文:</summary>
Something like this would work:
^[+-]?\d*\.*\d+(?:[+*\/-][+-]?\d*\.*\d+)*$
- `^` - start string anchor
- `[+-]?` - start with an optional positive or negative sign
- `\d*\.*\d+` - optional digit, followed by optional decimal, followed by required digit. This allows us to account for whole numbers or decimals.
- `(?:[+*\/-][+-]?\d*\.*\d+)*` - repeating optional pattern of previous two bullet points with a required preceding operator
- `$` - end string anchor
https://regex101.com/r/srl7vj/1
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论