-2

I know this seems a duplicated question but I can't find what I need.

I want to make a TextView (Android) where each word is clickable, but I need to match every sequence of characters unless whitespaces, so something like "hello!" or "«tomorrow" or "días" are valid matches for me.

So I don't need the pattern to find whitespaces, because I don't want split.

My scenario is Android but I only need the regexp, I'm playing with http://regexr.com/ to test and one example text to test is:

Con cien cañones por banda, viento en popa a toda vela, no corta el mar, sino vuela, un velero bergantín: bajel pirata que llaman por su bravura el Temido, en todo mar conocido del uno al otro confín. La luna en el mar riela, en la lona gime el viento, y alza en blando movimiento olas de plata y azul; y ve el capitán pirata, cantando alegre en la popa, Asia a un lado, al otro Europa y allá a su frente Stambul. «Navega, velero mío, sin temor, que ni enemigo navío, ni tormenta, ni bonanza, tu rumbo a torcer alcanza, ni a sujetar tu valor. «Veinte presas hemos hecho a despecho del inglés, y han rendido sus pendones cien naciones a mis pies. «¿Qué es mi barco? Mi tesoro. ¿Qué es mi Dios? La libertad. ¿Mi ley? ¡La fuerza y el viento! ¿Mi única patria? ¡La mar! «Allá muevan feroz guerra ciegos reyes por un palmo más de tierra: que yo tengo aquí por mío cuanto abarca el mar bravío, a quien nadie impuso leyes. «Y no hay playa sea cual quiera, ni bandera de esplendor, que no sienta mi derecho y dé pecho a mi valor. «¿Qué es mi barco? Mi tesoro. ¿Qué es mi Dios? La libertad. ¿Mi ley? ¡La fuerza y el viento! ¿Mi única patria? ¡La mar! «A la voz de «¡barco viene!» Es de ver cómo vira y se previene a todo trapo a escapar: que yo soy el rey del mar, y mi furia es de temer. «En las presas yo divido lo cogido por igual: sólo quiero por riqueza la belleza sin rival. «¿Qué es mi barco? Mi tesoro. ¿Qué es mi Dios? La libertad. ¿Mi ley? ¡La fuerza y el viento! ¿Mi única patria? ¡La mar! «¡Sentenciado estoy a muerte! Yo me río: no me abandone la suerte, y al mismo que me condena, colgaré de alguna antena, quizá en su propio navío. «Y si caigo, ¿qué es la vida? Por perdida ya la di cuando el yugo del esclavo, como un bravo, sacudí. «¿Qué es mi barco? Mi tesoro. ¿Qué es mi Dios? La libertad. ¿Mi ley? ¡La fuerza y el viento! ¿Mi única patria? ¡La mar! «Son mi música mejor aquilones; el estrépito y temblor de los cables sacudidos, del negro mar los bramidos y el rugir de mis cañones. «Y del trueno al son violento, y del viento al rebramar, yo me duermo sosegado. Arrullado por el mar. «¿Qué es mi barco? Mi tesoro. ¿Qué es mi Dios? La libertad. ¿Mi ley? ¡La fuerza y el viento! ¿Mi única patria? ¡La mar!

rubdottocom
  • 7,660
  • 10
  • 35
  • 56
  • Your example would be matched by [\wñáúéí]+ ... i guess you need some more lettersbut i think you can get the point – Jochen Schultz Jun 24 '15 at 08:10
  • @JochenSchultz I hope you agree with me that add any UTF-8 character to a regexp expression doesn't seems to be a good solution, but maybe I'm to newbie with regexp and is the only solution, there is no better approach? – rubdottocom Jun 24 '15 at 08:22

1 Answers1

2

You have almost answered your question by yourself.

"Every sequence of characters unless whitespaces" - it is exactly /([^\s])+/g

Andy Korneyev
  • 25,238
  • 15
  • 65
  • 65
  • 1
    I had go with `/\S+/g` if it's supported (negation of \s). Worth noting \s match space, tabs, carriage return, linefeed and newline. – Tensibai Jun 24 '15 at 08:13
  • 1
    [`\S` is supported](http://developer.android.com/reference/java/util/regex/Pattern.html). More, there is an answer to the question on that page (`\\S+` is used in a `replaceAll` though). – Wiktor Stribiżew Jun 24 '15 at 08:14
  • Thanks Andy, I know would be an easy one, but I can assure you that it was difficult find the answer there, too easy question that normally is answered with a .split sentence. – rubdottocom Jun 24 '15 at 08:29
  • @stribizhev and Tensibai in my scenario I will never have this special characters because text is procesed before I need to filter it. Btw, thx for the suggestion I'll take into account for future needs – rubdottocom Jun 24 '15 at 08:31