Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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_Regex_Pattern Matching - Fatal编程技术网

Java 使用正则表达式搜索带分隔符的键值字符串中的值

Java 使用正则表达式搜索带分隔符的键值字符串中的值,java,regex,pattern-matching,Java,Regex,Pattern Matching,我有一个分号分隔的键值对。如何通过提供键来搜索值。我需要这个用于Mule soft expression。我知道这可以在java中完成。我只寻找正则表达式来查找字符串 例如: abc=123;bcd=345;efg=567 如果我搜索abc它应该给我123 在正则表达式中如何执行此操作?它应该忽略/修剪值中的尾随空格。执行此操作的步骤: 首先使用将字符串拆分为令牌数组作为分隔符 对于每个令牌,第二个将其拆分为=,这里第一项是键,第二项是值 第三,将这些键值放入HashMap 使用map.Get

我有一个分号分隔的键值对。如何通过提供键来搜索值。我需要这个用于Mule soft expression。我知道这可以在java中完成。我只寻找正则表达式来查找字符串

例如:

abc=123;bcd=345;efg=567

如果我搜索abc它应该给我123

在正则表达式中如何执行此操作?它应该忽略/修剪值中的尾随空格。

执行此操作的步骤:

  • 首先使用
    将字符串拆分为令牌数组作为分隔符
  • 对于每个令牌,第二个将其拆分为
    =
    ,这里第一项是键,第二项是值
  • 第三,将这些键值放入
    HashMap
  • 使用
    map.Get()
    方法获取键的值
例如:

String data = "abc=123;bcd=345;efg=567";

HashMap<String, String> map = new HashMap<>();
for (String keyValue : data.split(";")) {
    String[] temp = keyValue.split("=", 2);
    map.put(temp[0], temp[1].trim());
}

System.out.println(map.get("abc"));
String data=“abc=123;bcd=345;efg=567”;
HashMap=newHashMap();
for(字符串keyValue:data.split(“;”)){
字符串[]temp=keyValue.split(“=”,2);
put(temp[0],temp[1].trim());
}
System.out.println(map.get(“abc”);
JAVA

没有必要使用
regex
,您可以使用
String
类中的
split()
方法来实现。下面是一个使用
的示例:

String line = "abc=123;bcd=345;efg=567";
HashMap<String, String> map = Arrays
    .stream(line.split(";")) //------------> splits the string where a semicolon is found
    .map(val -> val.split("=", 2)) // -----> splits and convert them to the map
    .collect(Collectors
        .toMap(curKey -> curKey[0], // -------> retrieves the key
               curVal -> curVal[1].trim(),//--> retrieves values by trimming white spaces
               (a, b) -> b, HashMap::new));//-> assigns the values

System.out.println(map.get("abc"));
例如:

String line = "abc=123;bcd=345;efg=567";
String search = "abc"; // -------------------------> key to search the chain
String regex = "([\\w]+)?=([\\w\\s]+)?;?"; // -----> regex expression
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    if (search.equals(matcher.group(1))){
        System.out.println(matcher.group(2).trim()); // ----> Gets the value
    }
}

输出:
123

我们可以用正则表达式来实现吗?我们可以在正则表达式中使用硬编码字符串吗?比如“给我abc=和;”之间的字符串?我们可以只使用正则表达式吗?我们可以在正则表达式中使用硬编码字符串吗?比如“给我abc=和;”之间的字符串?我得到了答案(?)?
String line = "abc=123;bcd=345;efg=567";
String search = "abc"; // -------------------------> key to search the chain
String regex = "([\\w]+)?=([\\w\\s]+)?;?"; // -----> regex expression
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    if (search.equals(matcher.group(1))){
        System.out.println(matcher.group(2).trim()); // ----> Gets the value
    }
}