Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中字符串外的数字_Java_String_Numbers - Fatal编程技术网

java中字符串外的数字

java中字符串外的数字,java,string,numbers,Java,String,Numbers,我有类似“ali123hgj”的东西。我想要123的整数。如何使用java实现它?使用以下RegExp(请参阅): 作者: final Pattern=Pattern.compile(\\d+);//正则表达式 final Matcher Matcher=pattern.Matcher(“ali123hgj”);//你的绳子 最终ArrayList ints=新ArrayList();//结果 而(matcher.find()){// int.add(Integer.parseInt(match

我有类似“ali123hgj”的东西。我想要123的整数。如何使用java实现它?

使用以下RegExp(请参阅):

作者:

final Pattern=Pattern.compile(\\d+);//正则表达式
final Matcher Matcher=pattern.Matcher(“ali123hgj”);//你的绳子
最终ArrayList ints=新ArrayList();//结果
而(matcher.find()){//
int.add(Integer.parseInt(matcher.group());//转换为int
}
请注意,这将如何将字符串不同部分的数字“合并”为一个数字。如果你只有一个号码,那么这个仍然有效。如果您只需要第一个数字,则可以执行以下操作:

int i = Integer.parseInt("x-42x100x".replaceAll("^\\D*?(-?\\d+).*$", "$1"));
// i == -42

正则表达式稍微复杂一点,但它基本上用它包含的第一个数字序列(可选减号)替换整个字符串,然后使用
Integer.parseInt
解析为整数。

您可能可以按照以下方式执行:

Pattern pattern = Pattern.compile("[^0-9]*([0-9]*)[^0-9]*");
Matcher matcher = pattern.matcher("ali123hgj");
boolean matchFound = matcher.find();
if (matchFound) {
    System.out.println(Integer.parseInt(matcher.group(0)));
}
它也很容易适应多个数字组。代码只是用于定位:它还没有经过测试。

int index=-1;
int index = -1;
for (int i = 0; i < str.length(); i++) {
   if (Character.isDigit(str.charAt(i)) {
      index = i; // found a digit
      break;
   }
}
if (index >= 0) {
   int value = String.parseInt(str.substring(index)); // parseInt ignores anything after the number
} else {
   // doesn't contain int...
}
对于(int i=0;i=0){ int value=String.parseInt(str.substring(index));//parseInt忽略数字后面的任何内容 }否则{ //不包含int。。。 }
公共静态最终列表扫描整数2(最终字符串源){
最终ArrayList结果=新建ArrayList();
//在现实生活中,将其定义为类的静态成员。
//将整数-123、12等定义为匹配项。
最终模式integerPattern=Pattern.compile(“(\\-?\\d+”);
最终匹配器匹配=整数模式匹配器(源);
while(matched.find()){
add(Integer.valueOf(matched.group());
}
返回结果;
在此输出中输入“asg123d ddhd-2222-33sds----222 ss---33dd 234”结果 [123,-2222,-33,-222,-33234]

就是这样

如果您只想匹配ASCII数字,请使用

如果您只想匹配拉丁字母表中的字母,请使用


那么
“abc123def567ghi”
“abcdef”
呢?数字前总是有3个字符,或者只是一个例子?它不仅仅是三个字符。它是介于0或更多字符之间的数字。它可以是“123”、“sdfs”、“123fdhf”、“fgdkjhgf123”
int i = Integer.parseInt("x-42x100x".replaceAll("^\\D*?(-?\\d+).*$", "$1"));
// i == -42
Pattern pattern = Pattern.compile("[^0-9]*([0-9]*)[^0-9]*");
Matcher matcher = pattern.matcher("ali123hgj");
boolean matchFound = matcher.find();
if (matchFound) {
    System.out.println(Integer.parseInt(matcher.group(0)));
}
int index = -1;
for (int i = 0; i < str.length(); i++) {
   if (Character.isDigit(str.charAt(i)) {
      index = i; // found a digit
      break;
   }
}
if (index >= 0) {
   int value = String.parseInt(str.substring(index)); // parseInt ignores anything after the number
} else {
   // doesn't contain int...
}
public static final List<Integer> scanIntegers2(final String source) {
    final ArrayList<Integer> result = new ArrayList<Integer>(); 
    // in real life define this as a static member of the class.
    // defining integers -123, 12 etc as matches.
    final Pattern integerPattern = Pattern.compile("(\\-?\\d+)");
    final Matcher matched = integerPattern.matcher(source);
    while (matched.find()) {
     result.add(Integer.valueOf(matched.group()));
    }
    return result;
String alphanumeric = "12ABC34def";

String digits = CharMatcher.JAVA_DIGIT.retainFrom(alphanumeric); // 1234

String letters = CharMatcher.JAVA_LETTER.retainFrom(alphanumeric); // ABCdef
String digits = CharMatcher.inRange('0', '9').retainFrom(alphanumeric); // 1234
String letters = CharMatcher.inRange('a', 'z')
                         .or(inRange('A', 'Z')).retainFrom(alphanumeric); // ABCdef