regex pattern

  • Thread starter Thread starter Pascal
  • Start date Start date
P

Pascal

I changed my mind in the maner of spliting a text into word to surround then
by a tag.
here is what i tried based on a javascript i use:

'This is what i use in javascript. How i can do this in vbnet?
'txt = txt.replace(/([\wéçâêîôûàèùëï]+)(?=[\s'".,;!?«»\-(\)])/gi,'<spanclass=\'blue\' onclick="verif(this)">$1<\/span>');

I tried something like this: but the pattern seems to have to be different
in vbnet

Private Sub BtnRegx_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles BtnRegx.Click
Dim input As String = TextBox1.Text
Dim pattern As String = "[\w]+ (?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">" + input + "</span>"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
TBoxRegx.Text = result

End Sub


Does someone knows th good pattern in vb?

thanks in advance
 
Pascal said:
I changed my mind in the maner of spliting a text into word to surround
then by a tag.
here is what i tried based on a javascript i use:

'This is what i use in javascript. How i can do this in vbnet?
'txt =
txt.replace(/([\wéçâêîôûàèùëï]+)(?=[\s'".,;!?«»\-(\)])/gi,'<spanclass=\'blue\'
onclick="verif(this)">$1<\/span>');

I tried something like this: but the pattern seems to have to be
different in vbnet

Private Sub BtnRegx_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles BtnRegx.Click
Dim input As String = TextBox1.Text
Dim pattern As String = "[\w]+ (?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">" + input + "</span>"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
TBoxRegx.Text = result

End Sub


Does someone knows th good pattern in vb?

thanks in advance

It's less different than you think. You have changed to pattern so that
it no longer catches the text, and you have changed the replacement
string so that it no longer uses what's caught. If you change them back
closer to the Javascript version, they will work:

Dim pattern As String = "([\w]+)(?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">$1</span>"

You may also need to set the "i" option:

rgx.Options = RegexOptions.IgnoreCase

The "g" option (global) isn't needed, as the Replace method by default
replaces all occurances.
 
Thanks a lot, you saved me!

I had tried this too without success :
'Dim pattern As String = "([\w]+)(?=[\s '.,;!?«»\-(\)])/gi"
'Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">""$1""</span>"

With your rectification, it works great!

Pascal



--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/

Göran Andersson said:
Pascal said:
I changed my mind in the maner of spliting a text into word to surround
then by a tag.
here is what i tried based on a javascript i use:

'This is what i use in javascript. How i can do this in vbnet?
'txt =
txt.replace(/([\wéçâêîôûàèùëï]+)(?=[\s'".,;!?«»\-(\)])/gi,'<spanclass=\'blue\'
onclick="verif(this)">$1<\/span>');

I tried something like this: but the pattern seems to have to be
different in vbnet

Private Sub BtnRegx_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles BtnRegx.Click
Dim input As String = TextBox1.Text
Dim pattern As String = "[\w]+ (?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">" + input + "</span>"
Dim rgx As New Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)
TBoxRegx.Text = result

End Sub


Does someone knows th good pattern in vb?

thanks in advance

It's less different than you think. You have changed to pattern so that it
no longer catches the text, and you have changed the replacement string so
that it no longer uses what's caught. If you change them back closer to
the Javascript version, they will work:

Dim pattern As String = "([\w]+)(?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">$1</span>"

You may also need to set the "i" option:

rgx.Options = RegexOptions.IgnoreCase

The "g" option (global) isn't needed, as the Replace method by default
replaces all occurances.
 
Hi again I test the code and only the first word is caught with options..
All the words are caught without options : i don't understand why? And You
thanks for an explanation
pascal

Private Sub BtnRegx_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles BtnRegx.Click

Dim input As String = TextBox1.Text
Dim pattern As String = "([\w]+)(?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">$1</span>"
Dim rgx As New Regex(pattern)
Dim options As RegexOptions = RegexOptions.IgnoreCase
Dim result As String = rgx.Replace(input, replacement, options)
TBoxRegx.Text = result

End Sub

and the text-test :

Les poules du couvent couvent l'obscurité du à la nuit et Noël.
Pourquoi? moi toi, eux lui-même!
radis nois: sérieux.

becomes :
<span class="blue" onclick="verif(this)">Les</span> poules du couvent
couvent l'obscurité du à la nuit et Noël.
Pourquoi? moi toi, eux lui-même!
radis nois: sérieux.
 
Pascal said:
Hi again I test the code and only the first word is caught with
options.. All the words are caught without options : i don't understand
why? And You thanks for an explanation
pascal

Private Sub BtnRegx_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles BtnRegx.Click

Dim input As String = TextBox1.Text
Dim pattern As String = "([\w]+)(?=[\s '.,;!?«»\-(\)])"
Dim replacement As String = "<span class=""blue""
onclick=""verif(this)"">$1</span>"
Dim rgx As New Regex(pattern)
Dim options As RegexOptions = RegexOptions.IgnoreCase
Dim result As String = rgx.Replace(input, replacement, options)
TBoxRegx.Text = result

End Sub

and the text-test :

Les poules du couvent couvent l'obscurité du à la nuit et Noël.
Pourquoi? moi toi, eux lui-même!
radis nois: sérieux.

becomes :
<span class="blue" onclick="verif(this)">Les</span> poules du couvent
couvent l'obscurité du à la nuit et Noël.
Pourquoi? moi toi, eux lui-même!
radis nois: sérieux.

There is no overload of the Replace method that takes two strings and
options, so you are calling some other overload where the options is
converted to something else. The possible candidates are that the
options is converted to an integer that limits the number of replaces,
or converted to a string, which would make the replacement the pattern
and the options the replacement.

(You should use OPTION STRICT ON so that the compiler won't convert
wildly between types.)

Set the options on the Regex object instead, as I suggested in the
previos reply:

Dim rgx As New Regex(pattern)
rgx.Options = RegexOptions.IgnoreCase
Dim result As String = rgx.Replace(input, replacement)
 
Back
Top