Java 正则表达式-分析给定字符串的数量
我有一个字符串(如文本短信),我想从中解析金额。但它只给出浮动量 示例字符串:-Java 正则表达式-分析给定字符串的数量,java,regex,Java,Regex,我有一个字符串(如文本短信),我想从中解析金额。但它只给出浮动量 示例字符串:- 您的账户188383xxx于20年8月18日贷记3000卢比。总资产余额23044.22卢比等等 您的账户188383xxx于20年8月18日贷记3000.33卢比。总金额23044.22卢比等等……” 我正在使用的正则表达式:- “(inr)+[\s]?+[0-9]+[\,]+[0-9]+[\.][0-9]{2}” 表达式的输出:- 字符串1)=23044.22卢比 字符串2)=卢比3000.33,卢比230
如果第一个字符串的结果是整数,我希望得到inr 3000。我缺少什么?这里最好的方法可能是使用正式的Java正则表达式模式匹配器,并迭代输入字符串以查找所有整数/浮点数:
String input = "Your account 188383xxxx is credited with inr 3000 on 18aug20. Total aval bal inr 23044.22 blah blah blah...";
String pattern = "\\binr\\s+(\\d+(?:\\.\\d+)?)\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
List<String> amounts = new ArrayList<>();
while (m.find()) {
amounts.add(m.group(1));
}
System.out.println(amounts);
使用的正则表达式模式是:
\binr\s+(\d+(?:\.\d+)?)\b
这意味着匹配一个整数或一个后跟小数部分的整数(即浮点数)。我们还在模式前面加上
inr
,以确保我们匹配的是卢比金额,而不是其他数字(例如,不是账号).这里最好的方法可能是使用正式的Java正则表达式模式匹配器,并迭代输入字符串以查找所有整数/浮点:
String input = "Your account 188383xxxx is credited with inr 3000 on 18aug20. Total aval bal inr 23044.22 blah blah blah...";
String pattern = "\\binr\\s+(\\d+(?:\\.\\d+)?)\\b";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(input);
List<String> amounts = new ArrayList<>();
while (m.find()) {
amounts.add(m.group(1));
}
System.out.println(amounts);
使用的正则表达式模式是:
\binr\s+(\d+(?:\.\d+)?)\b
这意味着匹配一个整数或一个后跟小数部分的整数(即浮点数)。我们还在模式前面加上
inr
,以确保我们匹配的是卢比金额,而不是其他数字(例如,不是账号)。可选正则表达式:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
上下文中的正则表达式:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
输出:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
可选正则表达式:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
上下文中的正则表达式:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
输出:
"\\binr\\s+([\\d\\.]+)"
public static void main(String[] args) {
String input = "Your account 188383xxxx is credited with bbbbinr 30022 inr 3000 on 18aug20."
+ " Total aval bal inr 23044.22 blah blah blah. In bookkeeping, an account refers to assets, \n"
+ "liabilities, income, expenses, and equity, as represented by individual\n"
+ "ledger pages, to which changes in value are chronologically recorded with\n"
+ " debit and credit entries. These entries, referred to as postings, \n"
+ "become part of a book of final entry or ledger. Examples of common financial\n"
+ " accounts are sales, accounts [1]receivable, mortgages, loans, PP&E, common \n"
+ "stock, sales, services, wages and payroll.\n"
+ "Your account 188383xxxx is credited with inr 3000.33 on 18aug20. Total aval bal"
+ " inr 23044.22 blah blah blah...";
Matcher matcher = Pattern.compile("\\binr\\s+([\\d\\.]+)").matcher(input);
while(matcher.find()) {
String amount = matcher.group(1);
System.out.println(amount); // Output is here :)
}
}
3000
23044.22
3000.33
23044.22
您需要将小数点后接两位数字的部分设置为可选。将该部分用括号括起来,并用一个问号(
?
)在结束括号之后。你能像写一个例子一样写吗?你需要让小数点后跟两位数字的部分成为可选部分。将该部分用括号括起来,并用一个问号(?
)在右括号之后。你能像一个例子那样写吗?如果那个帐号有xxxx,仅仅是因为OP试图屏蔽一个数字(在实际输入中,那些xxxx也是数字),则您的regexp将失败。OP作为regexp的一部分包含inr
。似乎是它的一个重要部分。@rzwitserroot我同意您的评论,我没有像我应该读的那样多地阅读问题。如果该帐号有xxxx,只是因为OP试图屏蔽一个数字(在实际输入中,这些xxxx也是数字),则您的regexp将失败。OP作为regexp的一部分包含在inr
中。似乎是其中的一个重要部分。@rzwitserroot我同意您的评论,我没有读到我应该读的那么多问题。