Extract words starting with a particular character from a string

﹥>﹥吖頭↗ 提交于 2019-12-07 09:28:23

问题


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.


回答1:


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+".

  • # the pattern start with a #
  • \w Matches any letter (a-z, A-Z), number (0-9), or underscore.
  • + Matches one or more consecutive \w characters.

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...




回答2:


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;


来源:https://stackoverflow.com/questions/29429074/extract-words-starting-with-a-particular-character-from-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!