Toon posts:

[VB6] Regular expressions probleem

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik ben sinds een paar dagen een beetje met regular expressions aan het experimenteren geweest in Visual Basic, nou kwam ik gisteren op het idee om regular expressions te gebruiken om HTML te highlighten. Dat lukte aardig, de tags kregen mooi allemaal een kleurtje. Toen wilde ik ook nog eens alles tussen " en " in een tag een andere kleur geven, ik had gehoopt dat dat ongeveer net zo ging als bij de tags zelf, maar dat is dus niet zo.

Ik doorloop de HTML tags stuk voor stuk, maar als je dus een tag als deze hebt:
code:
1
[img]"plaatje.gif"[/img]

dan wordt ook het stuk van het tweede tot het derde aanhalingsteken gehighlight(' alt='), dat is natuurlijk wel vrij logisch, die regular expression die ik nu gebruik highlight gewoon alles tussen twee aanhalingstekens. Hoe kan ik dit nu werkend krijgen?

Ik gebruik Visual Basic 6 en Microsoft VBScript Regular Expressions 5.5

Bij voorbaat dank

  • crisp
  • Registratie: Februari 2000
  • Laatst online: 00:21

crisp

Devver

Pixelated

1 woord: greediness

greedy:

code:
1
/"(.*)"/


ungreedy:

code:
1
/"(.*?)"/

Intentionally left blank


  • j_du_pee
  • Registratie: Maart 2000
  • Laatst online: 23-09-2024

j_du_pee

du pain, du vin, du pee

ik zou in mijn regx opnemen dat gehighlight wordt tussen =" en "spatie

dus zoiets:
"=""(.*)""\s|=""(.*)""\>"
uit de losse pols, dus weet niet of het werkt ;)

[ Voor 23% gewijzigd door j_du_pee op 12-09-2003 21:42 ]

kaart != map && bottel != fles
Wacht op antwoord


Verwijderd

Topicstarter
Geen van beide oplossing werkt nog, maar dat kan natuurlijk ook aan mij liggen. Ik zoek nog even verder en anders probeer ik wel een andere oplossing. In ieder geval bedankt voor de moeite :)

  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

hoe ... pas ... jij ... regexps ... toe ... in ... vb?

Zoiets zoek ik al tijden...

Of wacht, je gebruikt zeker dat Scripting Host dinges... mja, dan ben ik al niet meer geintresseerd :D

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea


  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 20-08 11:40

gorgi_19

Kruimeltjes zijn weer op :9

MUBA schreef op 13 September 2003 @ 17:38:
hoe ... pas ... jij ... regexps ... toe ... in ... vb?

Zoiets zoek ik al tijden...

Of wacht, je gebruikt zeker dat Scripting Host dinges... mja, dan ben ik al niet meer geintresseerd :D
Use Regular Expressions From VB6

Perhaps you're still working with VB5 or VB6 and don't plan to switch to .NET anytime soon. Fear not—you can still use regular expressions, thanks to a little-known library installed with Internet Explorer 5.0 and later versions. This library contains the code that enhances VBScript with regular expressions. Microsoft exposed its objects and methods in a type library that's embedded in the VBScript.dll file and accessible from any COM-enabled language.

The first step for using regular expressions from VB6 is adding a reference to this library, which is listed as Microsoft VBScript Regular Expression in the References dialog. The library contains only a few classes: RegExp (similar to the Regex in .NET), MatchCollection, Match, and SubMatches. SubMatches is similar to the GroupCollection .NET class and is available only in version 5.5 or later of the Regular Expression library.

The good news is that the regular expression constructs you can use from VB6 are a large subset of the ones available in .NET that I describe in this article. For example, most elements in Table 1 are supported in VB6 regular expressions, including character escapes, character classes, quantifiers, zero-width assertions, and named groups.

Although the regular-expression syntax is basically the same, the VB6 library contains fewer classes than its more powerful .NET counterpart, and method names are different in some cases. COM objects don't have constructors, so you must assign the Pattern and (optionally) the IgnoreCase property before processing the source string with the Execute method (which acts similarly to the Matches method under .NET). Another minor difference: You must assign the RegExp.Global property to True if you want to find all the occurrences of the pattern because the Execute method returns only the first occurrence by default. This code snippet shows how to proceed:

' Search all the words in a string
Dim re As New RegExp, m As Match
Dim t As String

t = "Hello, how are you?"
re.Pattern = "\w+"
re.Global = True
For Each m In re.Execute(t)
Print m.Value
Next

Gaining facility with regular expressions in VB6 will help you migrate your knowledge and applications easily when you're ready to switch to the .NET world.
Zie ook http://www.fawcette.com/v...magazine/features/balena/

Digitaal onderwijsmateriaal, leermateriaal voor hbo


  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

mja, toch die scripting host dinges... maar bedankt anyway :)

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea


Verwijderd

Topicstarter
Inderdaad, ik gebruik Microsoft VBScript Regular Expressions 5.5, d'r is volgens mij geen andere manier.

  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

mja die zijn er wel, maar allemaal commercieel

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea


  • j_du_pee
  • Registratie: Maart 2000
  • Laatst online: 23-09-2024

j_du_pee

du pain, du vin, du pee

Verwijderd schreef op 12 September 2003 @ 22:00:
Geen van beide oplossing werkt nog, maar dat kan natuurlijk ook aan mij liggen. Ik zoek nog even verder en anders probeer ik wel een andere oplossing. In ieder geval bedankt voor de moeite :)
hmm bij mij doet hij het wel :?
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Set objRegEx = New RegExp 
objRegEx.Global = True
objRegEx.IgnoreCase = True

objRegEx.Pattern = "=""(.*)""\s|=""(.*)""\>" 
Set objMatches = objRegEx.Execute("[img]""plaatje.gif""[/img]") 

If objMatches.Count > 0 Then
    For Each objMatch In objMatches
        WScript.StdOut.Write objMatch.submatches(0)  & vbcrlf & objMatch.submatches(1)
    Next
else
    WScript.StdOut.Write "geen lucifers"
end if


en krijg als ik hem run vanuit de dosprompt (cscript naam.vbs)

als output:
plaatje.gif

bla
tis nog niet gereplaced dan maar dat zou er op moeten lijken ;)
(wscript versie 5.6)
MUBA schreef op 13 September 2003 @ 17:38:
Of wacht, je gebruikt zeker dat Scripting Host dinges... mja, dan ben ik al niet meer geintresseerd :D
waarom niet eigenlijk? bang dat scriptblocking aanstaat oid?

kaart != map && bottel != fles
Wacht op antwoord


  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

j_du_pee schreef op 13 September 2003 @ 20:51:

waarom niet eigenlijk? bang dat scriptblocking aanstaat oid?
Nee, want de meeste programma's die ik schrijf, schrijf ik toch voor mezelf. Maar ik vind de manier die die script dinges gebruikt te omslachtig. Ik bedoel, ik heb regular expressions leren kennen toen ik Perl ging gebruiken. En in die taal is Regexp wat het is: een krachtige, korte manier om text te zoeken en te vervangen. Maar met die Microsoft-manier (geen flame ofzo in de richting van Microsoft, als het een ander bedrijf was geweest had ik de naam van het andere bedrijf gebruikt) is het weer onwijs veel gedoe eromheen, met het maken van objecten enzo ofzo.

Natuurlijk is dat niet heel moeilijk ofzo, maar het doet toch een beetje af aan de superieuriteit van regexps.

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea


  • muba
  • Registratie: April 2002
  • Laatst online: 19-10-2013

muba

Prince of Persia!

maar om ff te antwoorden op je vraag... het is inderdaad de greedyness en dat kan je verhelpen door achter de .* een vraagteken te plaatsen: .*?

Reporter: Mister Gandhi, what do you think of western civilisation?
Gandhi: I think it would be a good idea


Verwijderd

Topicstarter
j_du_pee schreef op 13 September 2003 @ 20:51:
Visual Basic:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Set objRegEx = New RegExp 
objRegEx.Global = True
objRegEx.IgnoreCase = True

objRegEx.Pattern = "=""(.*)""\s|=""(.*)""\>" 
Set objMatches = objRegEx.Execute("[img]""plaatje.gif""[/img]") 

If objMatches.Count > 0 Then
    For Each objMatch In objMatches
        WScript.StdOut.Write objMatch.submatches(0)  & vbcrlf & objMatch.submatches(1)
    Next
else
    WScript.StdOut.Write "geen lucifers"
end if
Dan werkt ie bij mij niet helemaal goed, maar zo wel:
Visual Basic:
1
objRegEx.Pattern = "=""(.*?)""\s|=""(.*?)""\>" 

Bedankt :)

[ Voor 35% gewijzigd door Verwijderd op 14-09-2003 01:19 ]

Pagina: 1