Regex how to get everything apart from the first character

This question already has an answer here:

  • How do you access the matched groups in a JavaScript regular expression? 14 answers
  • Java get matches group of Regex 2 answers

  • Javascript: (Originally the question was tagged with Javascript)

    The regex you have currently captures the value you want, but returns an array. The match without the equal sign is in the second element of the array:

    var str = "username=john903?fullname=john&smith".match(/=([^?]*)/)[1] 
    //str is now "john903"
    

    Java: (OP commented they are in fact using Java)

    String line = "username=john903?fullname=john&smith";
    Pattern pattern = Pattern.compile("=([^?]*)");
    Matcher matcher = pattern.matcher(line);
    System.out.println(matcher.group(1));
    

    You need to utilize positive lookbehind to achieve this. Reference

    and here's a fitting regex

    (?<==)([^?]*)
    

    [A-Za-z0-9]+(?=[?])
    

    This positive lookahead searches the characters behind the "?", excluding non letter and non numeric. It is a quick solution but may not work for every case.

    链接地址: http://www.djcxy.com/p/76808.html

    上一篇: 如何提取使用java的字符串正则表达式?

    下一篇: 正则表达式如何让所有的东西都从第一个字符中分离出来