Ik heb in ASP wat zitten oefenen met Regular expressions, bijv. om URL's en emailadressen binnen tekst te herkennen en om te zetten naar <A>, en om UBB-style tags te converteren. Probleem daarbij is dat het probeerbestand wat ik heb geschreven een beetje raar doet.
De expressies heb ik allemaal op eenzelfde manier geschreven, maar de ene Reg.exp doet het gewoon goed, terwijl anderen alleen de eerste match vinden, of zelfs helemaal geen. Bij allen staat Regexp.Global = True.
mijn vrij simpele oefenbestandje ziet er als volgt uit:
hier is dit bestand live te aanschouwen
source in txt formaat
let alsjeblieft niet op de vage tekst in de voorbeeldstring
ik word een beetje baldadig als iets niet lukt.
Ik heb de resultaten van elke Reg.Exp apart gezet, zodat je makkelijk kunt zien welke het goed doen en welke minder. Alleen de FindURLtag regexp doet het perfect, maar het is mij een raadsel waarom de FindU alleen maar de eerste match vindt, en verder niks.
Zitten er fouten in mijn patterns? Of ergens anders? Bij voorbaat hartelijk dank voor hulp/advies/verbeteringen
De expressies heb ik allemaal op eenzelfde manier geschreven, maar de ene Reg.exp doet het gewoon goed, terwijl anderen alleen de eerste match vinden, of zelfs helemaal geen. Bij allen staat Regexp.Global = True.
mijn vrij simpele oefenbestandje ziet er als volgt uit:
ASP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
| <% Dim Input, Output1, Output2, Output3, Output4, Output5, Output6, FindURL, FindB, FindURLtag, FindI, FindU, FindMail Input = "blabla cool[url=http://www.planetp01nt.com]Planet P01nt website[/url] blabloe piet@jan.com we are testing the [u]replacing methods[/u] using [i]regular expressions[/i] which are extremely fucking cool dude! [b]woohooo![/b] [u]out[i]in [/i]out[/u] some sample urls: http://www.dood.com supersimon33@hotmail.com [u] under lined[/u] [b]more bold[/b] adumbuser@inter.net jawel, [url=http://www.planetcrap.com]dit is een buttsite[/url]. en dit al helemaal: [url=http://www.powerunlimited.nl]PU site[/url]" Set FindMail = New RegExp 'zoekt naar strings met een @ erin, en een punt met daarna 2 of 3 letters aan 'het eind (domeinextensie) With FindMail .Pattern = "(\b[\w]+@[\w]+.[\w]{2,3})" .Global = True .IgnoreCase = True End With Set FindB = New RegExp 'zoekt naar [b] [/b] tags With FindB .Pattern = "\\[b\]([^\\[/b\]]+)\\[/b\]" .Global = True .IgnoreCase = True End With Set FindU = New RegExp 'zoekt naar [u] [/u] tags With FindU .Pattern = "\\[u\]([^\\[/u\]]+)\\[/u\]" .Global = True .IgnoreCase = True End With Set FindI = New RegExp 'zoekt naar [i] [/i] tags With FindI .Pattern = "\\[i\]([^\\[/i\]]+)\\[/i\]" .Global = True .IgnoreCase = True End With Set FindURLtag = New RegExp 'zoekt naar [url=http://www.site.com]site[/url] tags With FindURLtag .Pattern = "\\[url=(http://[^ \]]+)\]([^\\[]+)\\[\/url\]" .Global = True .IgnoreCase = True End With Set FindURL = New RegExp 'zoekt naar url's beginnend met http:\\ With FindMail .Pattern = " http:\/\/([^ @]+)" .Global = True .IgnoreCase = True End With Output1 = FindURLtag.Replace(Input, "<A HREF=$1>$2</A>") Output2 = FindURL.Replace(Input, "<A HREF=$1>$1</A>") Output3 = FindMail.Replace(Input, "<A HREF=mailto:$1>$1</A>") Output4 = FindB.Replace(Input, "<B>$1</B>") Output5 = FindI.Replace(Input, "<I>$1</I>") Output6 = FindU.Replace(Input, "<U>$1</U>") Call Response.Write(Output1 & "<BR><BR>") Call Response.Write(Output2 & "<BR><BR>") Call Response.Write(Output3 & "<BR><BR>") Call Response.Write(Output4 & "<BR><BR>") Call Response.Write(Output5 & "<BR><BR>") Call Response.Write(Output6 & "<BR><BR>") %> |
hier is dit bestand live te aanschouwen
source in txt formaat
let alsjeblieft niet op de vage tekst in de voorbeeldstring
Ik heb de resultaten van elke Reg.Exp apart gezet, zodat je makkelijk kunt zien welke het goed doen en welke minder. Alleen de FindURLtag regexp doet het perfect, maar het is mij een raadsel waarom de FindU alleen maar de eerste match vindt, en verder niks.
Zitten er fouten in mijn patterns? Of ergens anders? Bij voorbaat hartelijk dank voor hulp/advies/verbeteringen
[ Voor 19% gewijzigd door Not Pingu op 26-04-2003 21:12 ]
Certified smart block developer op de agile darkchain stack. PM voor info.