简体   繁体   English

Java正则表达式数字模式

[英]Java regex pattern for number

I know that this question can be stupid but I am trying to get some information from text and you are my last hope after last three hours of trying.. 我知道这个问题可能很愚蠢,但是我正在尝试从文本中获取一些信息,经过最后三个小时的尝试,您是我的最后希望。

DIC: C/40764176 IC: 407641'6 
Dekujerne a t8ime se na shledanou 

I need to get for example this 40764176 例如,我需要获取40764176

I need to get string with 8-10 length, sometimes there can be some special chars like I,i,G,S,O,ó,l) but I have tried a lot of patterns for this and no one works... 我需要获取长度为8-10的字符串,有时可能会有一些特殊的字符,例如I,i,G,S,O,ó,l),但是我为此尝试了很多模式,但是没有人可以使用...

I tried: 我试过了:

String generalDicFormatPattern = "([0-9IiGSOól]{8,10})";
String generalDicFormatPattern = ".*([0-9IiGSOól]{8,10}).*";
String generalDicFormatPattern = "\\b([0-9IiGSOól]{8,10})\\b";

nothing works... do you know where is the problem? 什么都行不通...您知道问题出在哪里吗?

edit: 编辑:

I use regex in this way: 我以这种方式使用正则表达式:

private List<String> getGeneralDicFromLine(String concreteLine) {
    List<String> allMatches = new ArrayList<String>();

        Pattern pattern = Pattern.compile(generalDicFormatPattern);
        Matcher matcher = pattern.matcher(concreteLine);

        while (matcher.find()) {             
             allMatches.add(matcher.group(1));
        }                           


    return allMatches;
}   

If your string's pattern is fixed you can use the regex 如果您的字符串模式是固定的,则可以使用正则表达式

C/([^\s]{8,10})\sIC:

Sample code: 样例代码:

String s = "DIC: C/40764176 IC: 407641'6";

Pattern p = Pattern.compile("C/([^\\s]{8,10})\\sIC:");
Matcher m = p.matcher(s);

if (m.find()) {
    System.out.println(m.group(1)); // 40764176
}

I'm expecting any character (includes the special ones you've shown in examples) but a white space. 我期待任何字符(包括您在示例中显示的特殊字符)但空格。

May be you can split your string with spaces ( string.split('\\\\s'); ), then you should have an array like this : 可能是您可以用空格( string.split('\\\\s'); )分割字符串,然后应该有一个像这样的数组:

  1. DIC: DIC:
  2. C/40764176 C / 40764176
  3. IC: 407641'6 IC:407641'6
  4. ... ...
  5. shledanou Shledanou

Get the second string, split it using '/', and get the second element. 获取第二个字符串,使用“ /”将其拆分,然后获取第二个元素。

I hope it helped you. 希望对您有所帮助。

Tip : you can check after the result using a regex ( ([0-9IiGSOól]{8,10} ) 提示:您可以使用正则表达式( ([0-9IiGSOól]{8,10} )查看结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM