英文:
Trying to get a specific word and the word after/before said word in a string
问题
"I'm trying to write a bot, and I ran into a problem that I cannot decide how to tackle.
For example, I have a string:
"Today i will buy a set of buns for 4214 usd and eat it tomorrow."
How do I see if the string has "usd" in it, and if it is an int/float? If it does, how do I get the number that is near it?
The numbers may vary in their form, but general recognizable forms should be as follows:
- 1000
- -1000
- 1000.0
- -1000.0
- 1000,0
- -1000
- 0
Any further divisions into parts shouldn't be recognized.
For the string above, the expected result would be:
"4214 usd"
The regular expression would be useful here, but I am new and not familiar with them at all."
英文:
I'm trying to write a bot, and I ran into a problem that I cannot decide how to tackle.
For example, I have a string:
> "Today i will buy a set of buns for 4214 usd and eat it tomorrow."
How do I see if the string has "usd" in it, and if it is an int/float? If it does, how do I get the number that is near it?
The numbers may vary in their form, but general recognizable forms should be as follows:
- 1000
- -1000
- 1000.0
- -1000.0
- 1000,0
- -1000
- 0
Any further divisions into parts shouldn't be recognized.
For the string above, the expected result would be:
"4214 usd"
The regular expression would be useful here, but I am new and not familiar with them at all.
答案1
得分: 0
Regex是一种可接受的工具,如果您可以接受偶尔的误报和漏报(错过的美元金额或错误标记为美元的情况)。
尝试这段代码。由于您要匹配字符串中的多种格式,一次性获取所有匹配项可能有些困难。
请参阅下面的注释以了解正则表达式的作用。
结果将给您一个字符串数组,因此您可以进行处理。例如,迭代它,然后去掉 usd
并将它们转换为JavaScript数字,这样您就可以对它们求和或进行其他操作。
英文:
Regex is an acceptable tool,, if you can live with occasional false positives and false negatives (missed dollar amounts or things incorrectly marked as dollars).
Try this code. Since you're matching multiple formats within a string, it's hard to get all the matches at once.
See the explanations in the comment below what does the regex do.
The result will give you an array of strings, so you can process it. For example, iterate it and then remove usd
and turn them into JS numbers, so you could sum them or whatever.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const text = `Today i will buy a set of buns for 4214 usd and eat it tomorrow. So tomorrow the budget would be total of -505.5 USD.`;
// regex to match "number USD"
const regex = /[-+]?\d+[,\.]?\d?\susd/gmi;
/*
[-+]? # optional hyphen or plus sign
\d+ # followed by multiple numbers
[,\.]? # followed by comma or dot, also optional
\d{1,2}? # followed by one or two numbers, optional
\s # followed by space
/gmi # global, multiline, case insensitive flags: match all occurences in input data, ignore case
*/
// execute
const result = text.match(regex);
console.log('matches: ', result);
// now you can do something, for turn strings to numbers for sum or whatever..
result.forEach(res => {
// turn it into a number or something
const num = res.replace(/\susd/i, '').replace(',', '.');
console.log(Number(num));
});
<!-- end snippet -->
答案2
得分: 0
.split
let str = "今天我将购买一套面包,价格为4214美元,明天吃掉它。";
let arr = str.split(" ");
if (arr.includes("美元")) {
arr.forEach(function (item, index) {
if (!isNaN(item)) {
console.log(item + " " + arr[index + 1]);
}
});
}
请注意,我已经将您提供的代码中的英文内容翻译成了中文。如果您需要进一步的帮助,请告诉我。
英文:
.split
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let str = "Today i will buy a set of buns for 4214 usd and eat it tomorrow.";
let arr = str.split(" ");
if(arr.includes("usd")){
arr.forEach(function(item,index){
if(!isNaN(item)){
console.log((item + " " + arr[index+1]));
}
})
}
<!-- end snippet -->
答案3
得分: -1
import re
def extract_amount(text):
pattern = r'(\d+(?:,\d+)*(?:\.\d+)?)\s*(usd)'
match = re.search(pattern, text, re.IGNORECASE)
if match:
amount = match.group(1)
currency = match.group(2)
return f"{amount} {currency}"
return None
text = "Today i will buy a set of buns for 4214 usd and eat it tomorrow."
result = extract_amount(text)
if result:
print(result)
else:
print("No amount found.")
英文:
import re
def extract_amount(text):
pattern = r'(\d+(?:,\d+)*(?:\.\d+)?)\s*(usd)'
match = re.search(pattern, text, re.IGNORECASE)
if match:
amount = match.group(1)
currency = match.group(2)
return f"{amount} {currency}"
return None
text = "Today i will buy a set of buns for 4214 usd and eat it tomorrow."
result = extract_amount(text)
if result:
print(result)
else:
print("No amount found.")
You can use that code block
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论