English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What I am trying to do is figure out the regex code needed to find the text between

and

tags so that I can extract it and write over it. So what I think would happen is that I would find the

tag, (which could also be

aparently, and then extract that text (just in case I need it later), and then be able to put text after this tag from a variable and have it be input before the

tag, (which could also be , or even). Any idea of the regex code as well as the class needed to do this in VB.NET? Here is what I have:

Dim allText As String = FileReader.ReadToEnd
Dim holdText As Match
Dim theregexpattern As String = "*"
Dim aregex As New Regex(theregexpattern)
holdText = aregex.Match(allText)

I unfortunatly can not get anything bigger than the

tag, (I want to get the text between the two tags and replace it. Notice I have a * in the regex but it is not picking up any of the characters between the two tags.

Thanks,

2007-08-10 18:53:11 · 1 answers · asked by Brian D 1 in Computers & Internet Programming & Design

I have been trying to figure out how to get the text between the tags and so I tried * but that doesn't even work for the text after the first tag. I realize I don't have the second tag's regex in the code, I have been trying to do it step by step but I can't even get a

Hello World found. I keep getting an "" string when I try and add the *. Furthermore once I can do this, (perhaps I am even going in the wrong direction as I just want to replace what is in between the tags with stuff that I have from a string variable). I have been doing much research on Regex but am still not sure how to do this, that is to just replace the text in between the two tags. Any ideas? Especially examples in this case as I just seem to be doing something wrong. Any help is much appreciated.

Thanks,

Brian

2007-08-10 18:59:12 · update #1

1 answers

Well, I hope in VB.NET there is the equivalent of MID$() or MID() in all versions of BASIC and Excel and othe spreadsheets. MID typically takes a beginning character postion and a number of characters like MID(str,beg,num). If the count is left off, most return from the beg char to the end, otherwise you have to fudge using the LEN of str.
So to find the beginning for MID, you need something like INSTR which looks like c=instr(str1, str2) where c is the first position of str2 in str1 like if instr("ABCDE", "CD") you would get 3. instr usually has an optional start search position like c=(first,str1, str2) to save using MID. One nice difference is that INSTR keeps the character count so if c=instr(4,"ABCDEAB","AB") it returns 6 the absolute location in the string, not 3, the relative position.
In your case, what you want to do (in my opinion) is to clean the source to get rid of etc. so you are dealing with

. Then you would do something like this
c1 = instr(source,"

") ' gets the location of beginning
if c1>0 then c2= instr(source,"

"
if c1>0 and c2>c1 then
holdstr = mid(source, c1+4, c2-c1+4) ' pull out text between parts adding length of

to c1 and subtracting to get length allowing for length of

.
source = mid(source, 1, c1+3)&newtext&mid(source,c2) ' [or ...&mid(source,c2, len(source)-c2) ' put new text between ends of old text in source.
else
flag=notfound
endif

2007-08-10 19:30:29 · answer #1 · answered by Mike1942f 7 · 0 0

fedest.com, questions and answers