>Ho trovato una espressione regolare molto interessante, ma non
>riesco ad utilizzarla nel modo corretto.
>
>Dim myRegex As New Regex("(?i)\bparola\b(?=[^<>]*(?:<\w|$))",
>RegexOptions.IgnoreCase Or RegexOptions.Compiled)
>
L'idea mi sembra buona, quella regular funziona bene, con qualche nota:
(?i) = asserisce che la regular deve essere case insensitive, quindi puoi omettere RegexOptions.IgnoreCase, in realtà io preferisco non utilizzare (?i) e specificare RegexOptions.IgnoreCase
RegexOptions.Compiled = è un ottimizzazione che permette a .net di compilare alla prima esecuzione un algoritmo di ricerca, senza doverla ricompilare ad ogni esecuzione, questo sempre essere sempre un vantaggio, ma non è così, dovresti fare dei load test, prima di aggiungere questa opzione per partito preso.
(?=[^<>]*(?:<\w|$)) = questa parte a cosa ti serve? in pratica se dopo la parola da cercare c'è un '<' o '>' il match salta, esempio 'adsdadd parola < asdasd' questo salta, invece lo prende se ad esempio è così 'asdasasda parola <adasda ldkalda', se dopo '<' ci sono caratteri o numeri
Tutta la regular è spiegata così (eng):
Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b»
Match the character string “parola” literally (case insensitive) «parola»
Assert position at a word boundary (position preceded or followed—but not both—by a Unicode letter, digit, or underscore) «\b»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[^<>]*(?:<\w|$))»
Match any single character NOT present in the list “<>” «[^<>]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the regular expression below «(?:<\w|$)»
Match this alternative (attempting the next alternative only if this one fails) «<\w»
Match the character “<” literally «<»
Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation) «\w»
Or match this alternative (the entire group fails if this one fails to match) «$»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed) «$»
a prescindere dalla regular, che semplificata potrebbe essere
new Regex("\bparola\b", RegexOptions.IgnoreCase)
devi applicarla agli articoli prima di stamparli sul browser, quindi come flusso pensavo ad una cosa del genere:
1 - query a db per ottenere l'articolo
2 - query a db per ottenere la lista delle parole conosciute e relativi link della fonte
3 - fai una bella regex.replace per ogni parola conosciuta, costruendo la regular con uno string.format, qualcosa del genere:
in pratica $+ verrà sostituito con la parola trovata.
poi il tutto può essere un po' ottimizzato, mettendo la query a db della lista delle parole conosciute in una cache per evitare di richiamare sempre il db.
Michael Denny | Visual C# MVP
http://blogs.dotnethell.it/Regulator/
http://dennymichael.wordpress.com
http://mvp.microsoft.com/mvp/Michael%20Denny-5000735
Twitter: @dennymic