尝试获取字符串中特定单词以及该单词之前/之后的单词。

huangapple go评论68阅读模式
英文:

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 &quot;number USD&quot;
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(&#39;matches: &#39;, result);


// now you can do something, for turn strings to numbers for sum or whatever..
result.forEach(res =&gt; {

  // turn it into a number or something
  const num = res.replace(/\susd/i, &#39;&#39;).replace(&#39;,&#39;, &#39;.&#39;);
  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 = &quot;Today i will buy a set of buns for 4214 usd and eat it tomorrow.&quot;;
let arr = str.split(&quot; &quot;);
if(arr.includes(&quot;usd&quot;)){
  arr.forEach(function(item,index){
    if(!isNaN(item)){
      console.log((item + &quot; &quot; + 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&#39;(\d+(?:,\d+)*(?:\.\d+)?)\s*(usd)&#39;
    match = re.search(pattern, text, re.IGNORECASE)
    
    if match:
        amount = match.group(1)
        currency = match.group(2)
        return f&quot;{amount} {currency}&quot;
    
    return None

text = &quot;Today i will buy a set of buns for 4214 usd and eat it tomorrow.&quot;
result = extract_amount(text)
if result:
    print(result)
else:
    print(&quot;No amount found.&quot;)

You can use that code block

huangapple
  • 本文由 发表于 2023年6月9日 00:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定