java - 使用正则表达式解析 URL 哈希

标签 java regex

需要解析这个字符串

#Login&oauth_token=theOAUTHtoken&oauth_verifier=12345

我只需要在哪里获取 oauth_tokenoauth_verifier键+值,使用正则表达式执行此操作的最简单方法是什么?

最佳答案

这样就可以了,你没有指定你想要的数据输出方式,所以我用逗号分隔它们。

import java.util.regex.*;

class rTest {
  public static void main (String[] args) {
    String in = "#Login&oauth_token=theOAUTHtoken&oauth_verifier=12345";
    Pattern p = Pattern.compile("(?:&([^=]*)=([^&]*))");
    Matcher m = p.matcher(in);
    while (m.find()) {
      System.out.println(m.group(1) + ", " + m.group(2));
    }
  }
}

正则表达式:

(?:           group, but do not capture:
  &           match '&'
   (          group and capture to \1:
    [^=]*     any character except: '=' (0 or more times)
   )          end of \1
   =          match '='
   (          group and capture to \2:
    [^&]*     any character except: '&' (0 or more times)
   )          end of \2
)             end of grouping

输出:

oauth_token, theOAUTHtoken
oauth_verifier, 12345

关于java - 使用正则表达式解析 URL 哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18301721/

相关文章:

java - 将输出分成 4 张椅子和蛮力方法的 block

java - 无法在 bash 脚本中运行 java

java - 进程生成器导致 error2 即使命令从 cmd 运行也找不到指定的路径

java - 我可以不这样做吗?收到 'exception' 错误

javascript - 在 JavaScript 中为特殊字符编写正则表达式

正则表达式匹配英国邮政编码的前半部分

java - 来自另一个 JFrame 的 JTable 值

java - 上传时,我想使用 Azure blob 存储方法 uploadFromFile() 覆盖现有 blob

php - 检查字母和/或整个单词是否包含在给定字符串中的函数

regex - 引用-此正则表达式是什么意思?