如果我有字符串
thisIsSomthing=4891\r\n
thisIsSomthingElse=27398472\r\n
thisIsNumber1=1\r\n
我怎么找到
thisIsNumber1
然后使用正则表达式返回 1
这假设您确实将发布的内容放在字符串中,而不是在文件中。在处理属性时,您应该使用Properties
而不是正则表达式:
String yourString = ...
Properties prop = new Properties();
try {
prop.load(new StringReader(yourString));
String result = prop.getProperty("thisIsNumber1");
System.out.println(result);
} catch (IOException e) {
System.out.println("Error loading properties:");
e.printStackTrace();
}
/thisIsNumber1=(\d+).*/
它将在捕获组 1 中。
String line="thisIsNumber1=1\r\n";
String temp=line.split("\\r?\\n")[0].split("=")[1];
System.out.println("Value="+temp+"*"); // 1* "*" shows nothing is concatenated after
the character in the output