简体   繁体   English

如何使用正则表达式查找字符串中子字符串的不同出现?

[英]How to find different occurrences of a substring in a string using regex?

Basically what I want to do is this: 基本上我想做的是这样的:

String mystr = "hello Benjamin Benny Benn hey hey";
String pattern = "Be";

Desired list = {"Benjamin","Benny","Benn"}; 所需列表= {“ Benjamin”,“ Benny”,“ Benn”};

I know how to do this in a really simple way. 我知道如何以一种非常简单的方式做到这一点。 What I am looking for is a fast way to do it based on regex or whatever that works for me. 我正在寻找的是一种基于正则表达式或对我有用的方法的快速方法。 I want a list of all sub-strings(words) starting with a specific pattern. 我想要以特定模式开头的所有子字符串(单词)的列表。

Use this regex: 使用此正则表达式:

Be\w+

What it does? 它能做什么?

It matches all the words that start with Be 它匹配以Be开头的所有单词

If you want word starting with anything else, simply do this: 如果您希望单词以其他任何内容开头,只需执行以下操作:

String startsWith="Be"; // change this to match your requirements

String regexPattern=startsWith+"\\w+"; //escaped backslash

Now, you can substitute anything in startsWith , so you can match words that starts with a specific string. 现在,您可以替换startsWith任何内容,以便匹配以特定字符串开头的单词。

Note: You'll need to escape the backslash in java. 注意:您需要在Java中转义反斜杠。 So \\ becomes \\\\ 所以\\变成\\\\

Try, 尝试,

String mystr = "hello Benjamin Benny Benn hey hey";
String pattern = "Be";
for(String str : mystr.split("\\s")){
    if(str.matches(pattern+"\\w+")){
         System.out.println("Matched "+str);
         // Add the str to list
    }
}

Here, 这里,

\\\\w+ indicates - \\\\w+表示-
word characters (az, AZ, 0-9, _) (1 or more times (matching the most amount possible)) 单词字符(az, AZ, 0-9, _) (1次或多次(匹配尽可能多的次数))

\\\\s indicates - \\\\s表示-
whitespace (\\n, \\r, \\t, \\f, and " ") 空格(\\n, \\r, \\t, \\f, and " ")

I found this answer which helped me a lot: 我找到了这个答案,这对我有很大帮助:

String yourString = "hi #how are # you";
Matcher matcher = Pattern.compile("#\\s*(\\w+)").matcher(yourString);    
while (matcher.find()) {
  System.out.println(matcher.group(1));
}

This way even if your pre-string is separated from the main string it works fine. 这样,即使您的前置字符串与主字符串分开,它也可以正常工作。

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

相关问题 正则表达式:如何用n次出现的子字符串替换字符串 - Regex: how to substitute a string with n occurrences of a substring 查找字符串中子字符串的出现次数 - Find the Number of Occurrences of a Substring in a String java - 如何使用模式和匹配器在java中查找子字符串="\\\\r" in a string="This is a \\\\\\\\rtest \\\\n\\\\r string" 的出现 - How to find occurrences of a substring="\\r" in a string="This is a \\\\\rtest \\n\\r string" in java using Pattern and matchers 如何查找给定字符串中所有出现的子字符串(允许使用通配符) - How to find all occurrences of a substring (with wildcards allowed) in a given String 如何在 Java 中查找和替换字符串中所有出现的子字符串? - How find and replace all occurrences of substring in string in Java? 使用正则表达式从复杂字符串中查找 substring - Find substring from a complex string using regex 使用正则表达式替换所有奇数出现的子字符串 - Replace all odd occurrences of a substring using regex 查找字符串中所有出现的分割子字符串 - Find all occurrences of a divided substring in a string 在Java中查找字符串中出现的所有子字符串 - Find all occurrences of substring in string in Java Java正则表达式在字符串中查找子字符串 - Java regex find substring in string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM