检测字符串是否与格式说明符字符串匹配
Detect if string matches format specifier string
我正在尝试执行反向格式化字符串的过程,即将现有字符串与类似字符串格式说明符字符串的 printf 相匹配。
我有一个包含多个字符串的数组,如下所示:
var strings = [
"I have the number 5 and the word dog",
"15 of your cows are belong to us"
];
我还有一个格式字符串数组,对应于这些字符串:
var formats = [
"I have the number %i and the word %s",
"%i of your %s are belong to us"
];
在我的特定用例中,我只需要匹配整数和单个单词字符串。
function getFormatString(string) {
//What I'm trying to accomplish
}
getFormatString(strings[0]); //returns formats[0];
getFormatString(strings[1]); //returns formats[1];
为每个格式字符串构建一个正则表达式似乎是我能想到的最好的主意,但我不确定如何去做。我还想使用格式字符串提取字符串的值。
将您的格式字符串修改为正则表达式:
- 将
%i
替换为(-?[\d]+)
- 将
%s
替换为(.*)
- 添加开始和结束标记
这是结果:
var formats = [
"^I have the number (-?[\d]+) and the word (.*)$",
"^(-?[\d]+) of your (.*) are belong to us$"
];
在您的函数中,您循环遍历格式字符串,直到其中一个匹配。
我和 Lorenz 的想法一样,因为它确实是唯一的选择。这是一个应该为您完成的脚本。
您也可以使用它来转换您的数组。我什至添加了转义百分号的功能(如果只有 js 的正则表达式支持 lookbehinds,这将是一个更容易的任务)。
var strings = [
"I have the number 5 and the word dog",
"15 of your cows are belong to us",
"1 of these strings should fail, because strings is matched against a %i."
];
var formats = [
"I have the number %i and the word %s",
"%i of your %s are belong to us",
"1 of these strings should fail, because %i is matched against a \%i."
];
var formex = [];
for (i=0; i<formats.length; i++) {
formex[i] = "^" + formats[i].replace(/([^\w\s\%])/g,"\").replace(/(^|[^\])%i/gi,"\(\d+\)").replace(/(^|[^\])%s/gi,"\([a-z]+\)").replace(/\%/g,"%") + "$";
}
console.log(formex);
for (i=0; i<formats.length; i++) {
console.log("Testing: <" + strings[i] + "> against <" + formex[i] + ">.");
console.log(strings[i].match(formex[i]));
}
我正在尝试执行反向格式化字符串的过程,即将现有字符串与类似字符串格式说明符字符串的 printf 相匹配。
我有一个包含多个字符串的数组,如下所示:
var strings = [
"I have the number 5 and the word dog",
"15 of your cows are belong to us"
];
我还有一个格式字符串数组,对应于这些字符串:
var formats = [
"I have the number %i and the word %s",
"%i of your %s are belong to us"
];
在我的特定用例中,我只需要匹配整数和单个单词字符串。
function getFormatString(string) {
//What I'm trying to accomplish
}
getFormatString(strings[0]); //returns formats[0];
getFormatString(strings[1]); //returns formats[1];
为每个格式字符串构建一个正则表达式似乎是我能想到的最好的主意,但我不确定如何去做。我还想使用格式字符串提取字符串的值。
将您的格式字符串修改为正则表达式:
- 将
%i
替换为(-?[\d]+)
- 将
%s
替换为(.*)
- 添加开始和结束标记
这是结果:
var formats = [
"^I have the number (-?[\d]+) and the word (.*)$",
"^(-?[\d]+) of your (.*) are belong to us$"
];
在您的函数中,您循环遍历格式字符串,直到其中一个匹配。
我和 Lorenz 的想法一样,因为它确实是唯一的选择。这是一个应该为您完成的脚本。
您也可以使用它来转换您的数组。我什至添加了转义百分号的功能(如果只有 js 的正则表达式支持 lookbehinds,这将是一个更容易的任务)。
var strings = [
"I have the number 5 and the word dog",
"15 of your cows are belong to us",
"1 of these strings should fail, because strings is matched against a %i."
];
var formats = [
"I have the number %i and the word %s",
"%i of your %s are belong to us",
"1 of these strings should fail, because %i is matched against a \%i."
];
var formex = [];
for (i=0; i<formats.length; i++) {
formex[i] = "^" + formats[i].replace(/([^\w\s\%])/g,"\").replace(/(^|[^\])%i/gi,"\(\d+\)").replace(/(^|[^\])%s/gi,"\([a-z]+\)").replace(/\%/g,"%") + "$";
}
console.log(formex);
for (i=0; i<formats.length; i++) {
console.log("Testing: <" + strings[i] + "> against <" + formex[i] + ">.");
console.log(strings[i].match(formex[i]));
}