Using Regular Expressions to Extract a Value in Java

前端 未结 13 1073
失恋的感觉
失恋的感觉 2020-11-22 13:08

I have several strings in the rough form:

[some text] [some number] [some more text]

I want to extract the text in [some number] using the

相关标签:
13条回答
  • 2020-11-22 13:40
    Pattern p = Pattern.compile("(\\D+)(\\d+)(.*)");
    Matcher m = p.matcher("this is your number:1234 thank you");
    if (m.find()) {
        String someNumberStr = m.group(2);
        int someNumberInt = Integer.parseInt(someNumberStr);
    }
    
    0 讨论(0)
  • 2020-11-22 13:41

    Look you can do it using StringTokenizer

    String str = "as:"+123+"as:"+234+"as:"+345;
    StringTokenizer st = new StringTokenizer(str,"as:");
    
    while(st.hasMoreTokens())
    {
      String k = st.nextToken();    // you will get first numeric data i.e 123
      int kk = Integer.parseInt(k);
      System.out.println("k string token in integer        " + kk);
    
      String k1 = st.nextToken();   //  you will get second numeric data i.e 234
      int kk1 = Integer.parseInt(k1);
      System.out.println("new string k1 token in integer   :" + kk1);
    
      String k2 = st.nextToken();   //  you will get third numeric data i.e 345
      int kk2 = Integer.parseInt(k2);
      System.out.println("k2 string token is in integer   : " + kk2);
    }
    

    Since we are taking these numeric data into three different variables we can use this data anywhere in the code (for further use)

    0 讨论(0)
  • 2020-11-22 13:44

    This function collect all matching sequences from string. In this example it takes all email addresses from string.

    static final String EMAIL_PATTERN = "[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})";
    
    public List<String> getAllEmails(String message) {      
        List<String> result = null;
        Matcher matcher = Pattern.compile(EMAIL_PATTERN).matcher(message);
    
        if (matcher.find()) {
            result = new ArrayList<String>();
            result.add(matcher.group());
    
            while (matcher.find()) {
                result.add(matcher.group());
            }
        }
    
        return result;
    }
    

    For message = "adf@gmail.com, <another@osiem.osiem>>>> lalala@aaa.pl" it will create List of 3 elements.

    0 讨论(0)
  • 2020-11-22 13:45
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class Regex1 {
        public static void main(String[]args) {
            Pattern p = Pattern.compile("\\d+");
            Matcher m = p.matcher("hello1234goodboy789very2345");
            while(m.find()) {
                System.out.println(m.group());
            }
        }
    }
    

    Output:

    1234
    789
    2345
    
    0 讨论(0)
  • 2020-11-22 13:49

    Full example:

    private static final Pattern p = Pattern.compile("^([a-zA-Z]+)([0-9]+)(.*)");
    public static void main(String[] args) {
        // create matcher for pattern p and given string
        Matcher m = p.matcher("Testing123Testing");
    
        // if an occurrence if a pattern was found in a given string...
        if (m.find()) {
            // ...then you can use group() methods.
            System.out.println(m.group(0)); // whole matched expression
            System.out.println(m.group(1)); // first expression from round brackets (Testing)
            System.out.println(m.group(2)); // second one (123)
            System.out.println(m.group(3)); // third one (Testing)
        }
    }
    

    Since you're looking for the first number, you can use such regexp:

    ^\D+(\d+).*
    

    and m.group(1) will return you the first number. Note that signed numbers can contain a minus sign:

    ^\D+(-?\d+).*
    
    0 讨论(0)
  • 2020-11-22 13:49

    Try doing something like this:

    Pattern p = Pattern.compile("^.+(\\d+).+");
    Matcher m = p.matcher("Testing123Testing");
    
    if (m.find()) {
        System.out.println(m.group(1));
    }
    
    0 讨论(0)
提交回复
热议问题