Extraer etiqueta hash de String [cerrado]

I want to extract any words immediately after the # personaje en un String, and store them in a String[] formación.

For example, if this is my String...

"Array is the most #important thing in any programming #language"

Then I want to extract the following words into a String[] formación...

"important"
"language"

Could someone please offer suggestions for achieving this.

preguntado el 03 de mayo de 12 a las 14:05

Should this be tagged as #homework? -

3 Respuestas

prueba esto -

String str="#important thing in #any programming #7 #& ";
Pattern MY_PATTERN = Pattern.compile("#(\\S+)");
Matcher mat = MY_PATTERN.matcher(str);
List<String> strs=new ArrayList<String>();
while (mat.find()) {
  //System.out.println(mat.group(1));
  strs.add(mat.group(1));
}

out put -

important
any
7
& 

respondido 25 mar '16, 18:03

How can i extract this words into a String[] array ? - baraaalsafty

@baraaalsafty - code updated (used List instead of Array) - Subhrajyoti Majumder

I see two variables called "str" @SubhrajyotiMajumder. Am I missing something? - Fersarr

This regex doesn't works with UTF-8, do not use it in real projects! :) - Ivan Zelenskiy

@IvanZelenskyy can offer a working alternative? - Sirvón

String str = "Array is the most #important thing in any programming #language";
Pattern MY_PATTERN = Pattern.compile("#(\\w+)");
Matcher mat = MY_PATTERN.matcher(str);
while (mat.find()) {
        System.out.println(mat.group(1));
}

La expresión regular utilizada es:

#      - A literal #
(      - Start of capture group
  \\w+ - One or more word characters
)      - End of capture group

contestado el 03 de mayo de 12 a las 14:05

How can i extract this words into a String[] array ? - baraaalsafty

Great! It ignores other chars like , - Salim Shamim

Prueba esta expresión regular

#\w+

contestado el 03 de mayo de 12 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.