📌  相关文章
📜  如何查找给定字符串中任何货币符号的索引

📅  最后修改于: 2021-04-28 17:31:06             🧑  作者: Mango

给定字符串txt ,任务是查找给定字符串存在的货币符号的索引。
例子:

天真的方法:
解决问题的最简单方法是执行以下操作:

  • 创建一组所有货币。
  • 遍历字符串,如果在字符串找到集合中存在的任何货币符号,则打印其索引。

上述方法需要辅助空间来存储集合中的所有货币。

高效方法:

  1. 这个想法是使用正则表达式来解决这个问题。
  2. 创建一个正则表达式以在字符串查找货币符号,如下所述:
    regex =“ \\ p {Sc} ”;
    在哪里:
    {\\ p {Sc}
    代表任何货币符号。
    对于C++ / Python,我们可以使用regex =“ \\ $ | \\£| \\€
    正则表达式检查字符串是否存在任何给定的货币符号($,£,€)。
  3. 使用Pattern.matcher()将给定的字符串与正则表达式匹配。
  4. 找到与给定正则表达式匹配的字符串的字符的打印索引。

下面是上述方法的实现:

C++
// C++ program to find indices of
// currency symbols present in a
// string using regular expression
#include 
#include 
using namespace std;
 
// Function to find currency symbol
// in a text using regular expression
void findCurrencySymbol(string text)
{
 
  // Regex to find any currency
  // symbol in a text
  const regex pattern("\\$|\\£|\\€");
  for (auto it = sregex_iterator(text.begin(), text.end(), pattern);
       it != sregex_iterator(); it++)
  {
     
      // flag type for determining the matching behavior
      // here it is for matches on 'string' objects
      smatch match;
      match = *it;
      cout << match.str(0) << " - " << match.position(0) << endl;
  }
  return ;
}
 
// Driver Code
int main()
{
  string txt
      = "$27 - $21.30equal to $5.70";
  findCurrencySymbol(txt);
  return 0;
}
 
// This code is contributed by yuvraj_chandra


Java
// Java program to find indices of
// currency symbols present in a
// string using regular expression
import java.util.regex.*;
class GFG {
 
    // Function to find currency symbol
    // in a text using regular expression
    public static void findCurrencySymbol(
        String text)
    {
 
        // Regex to find any currency
        // symbol in a text
        String regex = "\\p{Sc}";
 
        // Compile the ReGex
        Pattern p = Pattern.compile(
            regex);
 
        // Find match between the
        // given string and the
        // Regex using Pattern.matcher()
        Matcher m = p.matcher(text);
 
        // Find the next subsequence
        // of the input subsequence
        // that matches the pattern
        while (m.find()) {
 
            System.out.println(
                text.charAt(m.start())
                + " - "
                + m.start());
        }
    }
    // Driver Code
    public static void main(String args[])
    {
        String txt = "$27 - $21.30"
                    + "equal to $5.70";
        findCurrencySymbol(txt);
    }
}


Python3
# Python program to find indices of
# currency symbols present in a
# string using regular expression
import re
 
# Function to find currency symbol
# in a text using regular expression
def findCurrencySymbol(text):
 
    # Regex to find any currency
    # symbol in a text
    regex = "\\$|\\£|\\€"
 
    for m in re.finditer(regex, text):
        print(text[m.start(0)], "-" ,m.start(0))
 
 
# Driver code
txt = "$27 - $21.30equal to $5.70"
findCurrencySymbol(txt)
 
# This code is contributed by yuvraj_chandra


输出:

$ - 0
$ - 6
$ - 21

时间复杂度:O(N)

辅助空间:O(1)