简体   繁体   中英

Extract words starting with a particular character from a string

I got the following string:

 String line = "#food was testy. #drink lots of. #night was fab. #three #four";

I want to take #food #drink #night #three and #four from it.

I tried this code:

    String[] words = line.split("#");
    for (String word: words) {
        System.out.println(word);
    }

But it gives food was testy , drink lots of , nigth was fab , three and four .

split will only cuts the whole string at where it founds a # . That explain your current result.

You may want to extract the first word of every pieces of string, but the good tool to perform your task is RegEx

Here how you can achieve it:

String line = "#food was testy. #drink lots of. #night was fab. #three #four";

Pattern pattern = Pattern.compile("#\\w+");

Matcher matcher = pattern.matcher(line);
while (matcher.find())
{
    System.out.println(matcher.group());
}

Output is:

#food
#drink
#night
#three
#four

The magic happen in "#\\w+".

So we search for stuff starting with # followed by one or more letter, number or underscore.

We use '\\\\' for '\\' because of Escape Sequences .

You can play with it here .

find and group are explained here :

  • The find method scans the input sequence looking for the next subsequence that matches the pattern.
  • group() returns the input subsequence matched by the previous match.

[edit]

The use of \\w can be an issue if you need to detect accented characters or non-latin characters.

For example in:

"Bonjour mon #bébé #chat."

The matches will be:

  • #b
  • #chat

It depends on what you will accept as possible hashTag . But it is an other question and multiple discussions exist about it .

For example, if you want any characters from any language, #\\p{L}+ looks good, but the underscore is not in it...

Please follow the procedure to do ==>

   String candidate = "#food was testy. #drink lots of. #night was fab. #three #four";

        String regex = "#\\w+";
        Pattern p = Pattern.compile(regex);

        Matcher m = p.matcher(candidate);
        String val = null;

        System.out.println("INPUT: " + candidate);

        System.out.println("REGEX: " + regex + "\r\n");

        while (m.find()) {
          val = m.group();
          System.out.println("MATCH: " + val);
        }
        if (val == null) {
          System.out.println("NO MATCHES: ");
        }

which will give output as follows as i solved the problem at my netbeans IDE and tested the program

INPUT: #food was testy. #drink lots of. #night was fab. #three #four

REGEX: #\w+

MATCH: #food

MATCH: #drink

MATCH: #night

MATCH: #three

MATCH: #four

you will need the following imports

import java.util.regex.Matcher;
import java.util.regex.Pattern;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM