[英]java regex capturing 2 numbers
我正在尋找一種方法來捕獲字符串的年份和最后一個數字。 例如:“01/02/2017,546.12,24.2”,到目前為止,我的問題只有 Found value : 2017 和 Found value : null。 我無法捕獲組(2)。 謝謝
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
public class Bourse {
public static void main( String args[] ) {
Scanner clavier = new Scanner(System.in);
// String to be scanned to find the pattern.
String line = clavier.nextLine();
String pattern = "(?<=\\/)(\\d{4})|(\\d+(?:\\.\\d{1,2}))(?=,$)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}
試試這個:
(\\d{2}\\.?\\d{2})
\\\\d{2}
- 正好兩位數\\\\.?
- 可選點\\\\d{2}
- 正好兩位數如果我理解正確,您正在尋找 4 位數字,可以用點分隔。
你的要求不是很清楚,但這對我來說只是簡單地獲取年份和最后一個十進制值:
Pattern pattern = Pattern.compile("[0-9]{2}/[0-9]{2}/([0-9]{4}),[^,]+,([0-9.]+),");
String text = "01/02/2017,546.12,24.2,";
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
String year = matcher.group(1);
String lastDecimal = matcher.group(2);
System.out.println("Year "+year+"; decimal "+lastDecimal);
}
我不知道您是否故意使用后視和前瞻,但我認為明確指定完整日期模式並使用兩個顯式逗號字符之間的值更簡單。 (顯然,如果您需要逗號保持播放狀態,您可以用前瞻替換最后一個逗號。)
順便說一句,我不喜歡\\d
速記,因為在許多語言中,這將匹配整個 Unicode 字符空間中的所有數字字符,而通常只需要匹配 ASCII 數字 0-9。 (Java 只在使用\\d
時匹配 ASCII 數字,但我仍然認為這是一個壞習慣。)
只需在逗號分隔符上拆分字符串即可。
String input = "01/02/2017,546.12,24.2,";
String[] parts = input.split( "," );
將每個元素解析為一個有意義的對象,而不是將所有內容都視為文本。
對於僅日期值,現代方法使用 Java 8 及更高版本中內置的java.time.LocalDate
類。
// Parse the first element, a date-only value.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "dd/MM/uuuu" );
LocalDate localDate = null;
String inputDate = parts[ 0 ] ;
try
{
localDate = LocalDate.parse( inputDate , f );
} catch ( DateTimeException e )
{
System.out.println( "ERROR - invalid input for LocalDate: " + parts[ 0 ] );
}
對於精度很重要的帶小數的數字,請避免使用浮點類型,而是使用BigDecimal
。 鑒於您的班級名稱“Bourse”,我認為這些數字與金錢有關,因此准確性很重要。 始終使用BigDecimal
處理金錢問題。
// Loop the numbers
List < BigDecimal > numbers = new ArrayList <>( parts.length );
for ( int i = 1 ; i < parts.length ; i++ )
{ // Start index at 1, skipping over the first element (the date) at index 0.
String s = parts[ i ];
if ( null == s )
{
continue;
}
if ( s.isEmpty( ) )
{
continue;
}
BigDecimal bigDecimal = new BigDecimal( parts[ i ] );
numbers.add( bigDecimal );
}
提取您想要的兩條信息:年份和最后一個數字。
考慮在您的代碼中傳遞一個Year
對象,而不僅僅是一個表示年份的整數。 這為您提供了類型安全性並使您的代碼更具自文檔性。
// Goals: (1) Get the year of the date. (2) Get the last number.
Year year = Year.from( localDate ); // Where possible, use an object rather than a mere integer to represent the year.
int y = localDate.getYear( );
BigDecimal lastNumber = numbers.get( numbers.size( ) - 1 ); // Fetch last element from the List.
轉儲到控制台。
System.out.println("input: " + input );
System.out.println("year.toString(): " + year );
System.out.println("lastNumber.toString(): " + lastNumber );
輸入:01/02/2017,546.12,24.2,
year.toString(): 2017
lastNumber.toString(): 24.2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.