英文:
Regular expression for extract numbers from the string
问题
I need to extract numbers from the string for that I have preferred the following regex
var str = "1 1/2 percentage";
var n = str.match(/[0-9]+([\/.]\d+)?/g);
console.log(n);
But I am not able to restrict negative numbers in the string .
Valid Scenario:
"1.5 % dividend applied"
"1 1/2 percentage"
"1/10 percentage"
"2.5 percentages"
"10% dividend"
"10 percentages applied"
Invalid Scenario:
"-1 1/2 percentage"
"-1.5 % dividend applied"
"-10% dividend"
Code suggestion will be appreciated. Thank you.
英文:
I need to extract numbers from the string for that I have preferred the following regix
var str = "1 1/2 percentage";
var n = str.match(/[0-9]+([\/.]\d+)?/g);
console.log(n);
But I am not able to restrict negative numbers in the string .
Valid Scenario:
"1.5 % dividend applied"
"1 1/2 percentage"
"1/10 percentage"
"2.5 percentages"
"10% dividend"
"10 percentages applied"
Invalid Scenario:
"-1 1/2 percentage"
"-1.5 % dividend applied"
"-10% dividend"
Code suggestion will be appriciated.Thank you
答案1
得分: 3
以下是您要翻译的内容:
(?<![-.\d]) # 匹配不以'-'、'.'或数字开头的内容,
\d+(?:\.\d+)? # 以数字开头,可包含小数点,
(?:\/\d+(?:\.\d+)?)? # 可选地跟着'/'和类似于第一个的另一个数字。
您可以在 regex101.com 上尝试。
尝试它:
// 隐藏: true 控制台: false babel: false
// 语言: lang-js
console.config({ maximize: true });
const testcases = [
'1.5 % dividend applied',
'1 1/2 percentage',
'1/10 percentages',
'2.5 percentages',
'10% dividend',
'18.6 foo',
'10 percentages applied',
'-1 1/2 percentage',
'-1.5 % dividend applied',
'-10% dividend',
'-18.6 foo',
'2.25/150 bar'
];
const regex = /(?<![-.\d])\d+(\.\d+)?(?:\/\d+(\.\d+)?)?/g;
for (const testcase of testcases) {
console.log(`'${testcase}'`, testcase.match(regex) ?? 'no match');
}
<!-- 语言: lang-html -->
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
原始答案:
(?<![-.\d]) # 匹配不以'-'、'.'或数字开头的内容,
\d+ # 以1个或更多数字开头,
([\/.]\d+)? # 以'/'或'.'后跟1个或更多数字结尾
您可以在 regex101.com 上尝试。
英文:
You need:
(?<![-.\d]) # Match something that is not preceded by '-', '.' or a digit,
\d+(?:\.\d+)? # starts with a number, with or without decimal places,
(?:\/\d+(?:\.\d+)?)? # optionally followed by '/' and another number similar to the first.
Try it on regex101.com.
Try it:
<!-- begin snippet: js hide: true console: false babel: false -->
<!-- language: lang-js -->
console.config({ maximize: true });
const testcases = [
'1.5 % dividend applied',
'1 1/2 percentage',
'1/10 percentages',
'2.5 percentages',
'10% dividend',
'18.6 foo',
'10 percentages applied',
'-1 1/2 percentage',
'-1.5 % dividend applied',
'-10% dividend',
'-18.6 foo',
'2.25/150 bar'
];
const regex = /(?<![-.\d])\d+(\.\d+)?(?:\/\d+(\.\d+)?)?/g;
for (const testcase of testcases) {
console.log(`'${testcase}'`, testcase.match(regex) ?? 'no match');
}
<!-- language: lang-html -->
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<!-- end snippet -->
Original answer
You need:
(?<![-.\d]) # Match something that is not preceded by '-', '.' or a digit,
\d+ # starts with 1+ digits, and
([\/.]\d+)? # ends with either a '/' or a '.' followed by 1+ digits
Try it on regex101.com.
Try it:
<!-- begin snippet: js hide: true -->
<!-- language: lang-js -->
console.config({ maximize: true });
const testcases = [
'1.5 % dividend applied',
'1 1/2 percentage',
'1/10 percentages',
'2.5 percentages',
'10% dividend',
'18.6 foo',
'10 percentages applied',
'-1 1/2 percentage',
'-1.5 % dividend applied',
'-10% dividend',
'-18.6 foo'
];
const regex = /(?<![-.\d])\d+([\/.]\d+)?/g;
for (const testcase of testcases) {
console.log(`'${testcase}'`, testcase.match(regex) ?? 'no match');
}
<!-- language: lang-html -->
<script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<!-- end snippet -->
答案2
得分: 0
用JavaScript和正则表达式混合编写的代码如下:
function getNumbers(text) {
// 匹配所有数字
var matches = text.match(/-?\d+([\/.]\d+)?/g);
var res = [];
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
if (!match.startsWith('-')) { // 检查是否为负数
res.push(match);
}
}
return res;
}
英文:
With a mix of javascript and regex:
function getNumbers(text) {
//match all numbers
var matches = text.match(/-?\d+([\/.]\d+)?/g);
var res = [];
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
if (!match.startsWith('-')) { // check if its not a negative number
result.push(match);
}
}
return res;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论