Find sentences starting and ending with a hash

I am working on discovering sentences that begin and end with hashtags. At the moment, I only have the code to find the words that are part of this mechanism. How to find offers depending on the case below.

Case 1:

Hello, #how are you# today. 

In this case, I want to discover how are you. Now, if there is only a word, then this case should be ignored.

Case 2:

Hello, #how are you #today. 

In this case, only words #howand #todayare found that I already have. No suggestions here, as words do not end with a hashtag.

code:

@Override
public List<String> findHashTags(String text){
    if(text == null){
        return new ArrayList<>();
    }
    String[] tagSet = text.split(" ");
    Set<String> sortedTags = new HashSet<>();
    List<String> processedTags = new ArrayList<>();
    for(String tags : tagSet){
         if(tags.startsWith("#")){
             sortedTags.add(tags);
         }
    }
    processedTags.addAll(sortedTags);
    return processedTags;
}

Updated Code

@Override
    public List<String> findHashTags(String text){
        if(text == null){
            return new ArrayList<>();
        }
        Set<String> sortedTags = new HashSet<>();
        List<String> processedTags = new ArrayList<>();
        Pattern pattern = Pattern.compile("#\\b.*?\\b#|\\B#\\w+");
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()){
            String outString = matcher.group();
            outString = outString.replace("#","");
            outString = outString.replace(",","");
            sortedTags.add(outString);
        }
        processedTags.addAll(sortedTags);

        return processedTags;
    }
+6
source share
2 answers

#, char #, char, #, char, 1+ .

#\b.*?\b#|\B#\w+

regex

, #_ s#, \b (?=\p{L})/(?=[a-zA-Z]), .

. Java-:

List<String> results = new ArrayList<>();
String s = "Hello, #how are you# today. Hello, #how are you #today.";
Pattern pattern = Pattern.compile("#\\b.*?\\b#|\\B#\\w+");
Matcher matcher = pattern.matcher(s);
while (matcher.find()){
    results.add(matcher.group());
} 
System.out.println(results); 
// => [#how are you#, #how, #today]
+6
String line = "#how are you #today.";     
String pattern = "#(.*?)#(.*?)";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(line);

while (m.find()) {
    System.out.println("Matching hash: " + m.group(1));
}
-3

All Articles