How i read text between specific begin & end tags?

Im trying to do it with RegEx and i am noob in it. Help!

Page 1 of 1

11 Replies - 7769 Views - Last Post: 14 August 2009 - 09:20 PM Rate Topic: **--- 1 Votes

#1 keksinnot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

How i read text between specific begin & end tags?

Post icon  Posted 01 August 2009 - 01:15 PM

I want to read ALL text between specific <b1> and </b1> tags + hide the tags, example text:

<b1>
As part of the Visual Studio product range, Microsoft created a set of free development environments for hobbyists and novices, the Visual Studio 2005 Express series. One edition in the series is Visual Basic 2005 Express Edition, which was succeeded by Visual Basic 2008 Express Edition in the 2008 edition of Visual Studio Express.[8]

The Express Editions are targeted specifically for people learning a language. They have a streamlined version of the user interface, and lack more advanced features of the standard versions. On the other hand, Visual Basic 2005 Express Edition does contain the Visual Basic 6.0 converter, so it is a way to evaluate feasibility of conversion from older versions of Visual Basic.
</b1>


I have tried the following, but i cant figure out what's wrong. I know it isn't impossible.
Dim inputString As String = RichTextBox1.Text
		Dim regex As New Regex("[<]b1[>]""[<]/b1[>]"(RegexOptions.Compiled))
		Dim col As MatchCollection = Regex.Matches(inputString)
		For Each match As Match In col
			Console.WriteLine("[<]b1[>]" + match.Groups("[<]b1[>]").Value)
		Next



I want to use .rtf because i need to save color etc. information, BUT if you know better way to do this; please tell me. B)


Thanks for your help, it's appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How i read text between specific begin & end tags?

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1998
  • View blog
  • Posts: 8,808
  • Joined: 29-May 08

Re: How i read text between specific begin & end tags?

Posted 01 August 2009 - 02:42 PM

RegEx can be a pain in <beep> so I use another program (Expresso).
Develop the RegEx in that then export into Visual Studio.
Was This Post Helpful? 0
  • +
  • -

#3 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How i read text between specific begin & end tags?

Posted 01 August 2009 - 05:02 PM

It looks like you're getting just the text from the RichTextBox, you need to use the Rtf property to get the raw rich text.
Dim inputString As String = RichTextBox1.Rtf


Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How i read text between specific begin & end tags?

Posted 01 August 2009 - 05:37 PM

If you're wanting to get text between a certain tag I personally would use RegularExpressions, like this

Dim inputString As String = RichTextBox1.Text
Dim pattern As String = "(?<=\<b1\>).+?(?=\<\/b1\>)"
Dim col As MatchCollection = Regex.Matches(inputString, pattern)
For Each match As Match In col
		Console.WriteLine(match.Groups(1).Value)
Next


Was This Post Helpful? 0
  • +
  • -

#5 keksinnot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: How i read text between specific begin & end tags?

Posted 02 August 2009 - 05:07 AM

View PostPsychoCoder, on 1 Aug, 2009 - 04:37 PM, said:

If you're wanting to get text between a certain tag I personally would use RegularExpressions, like this

Dim inputString As String = RichTextBox1.Text
Dim pattern As String = "(?<=\<b1\>).+?(?=\<\/b1\>)"
Dim col As MatchCollection = Regex.Matches(inputString, pattern)
For Each match As Match In col
		Console.WriteLine(match.Groups(1).Value)
Next




Did you try it? I still get nothing as result, maybe i am not handling the output correctly. How do i get the remaining text to appear? :blink:
Was This Post Helpful? 0
  • +
  • -

#6 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How i read text between specific begin & end tags?

Posted 02 August 2009 - 07:24 AM

You're still using
Dim inputString As String = RichTextBox1.Text

Instead of
Dim inputString As String = RichTextBox1.Rtf

Nobody ever listens to me. :sad3:
Was This Post Helpful? 0
  • +
  • -

#7 keksinnot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: How i read text between specific begin & end tags?

Posted 02 August 2009 - 09:10 AM

View PostLoveIsNull, on 2 Aug, 2009 - 06:24 AM, said:

You're still using
Dim inputString As String = RichTextBox1.Text

Instead of
Dim inputString As String = RichTextBox1.Rtf

Nobody ever listens to me. :sad3:


Result is still nothing, so this is not the main reason. :cry2:
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 1998
  • View blog
  • Posts: 8,808
  • Joined: 29-May 08

Re: How i read text between specific begin & end tags?

Posted 02 August 2009 - 10:56 AM

Post the current version of the code you are using.
Can you be more descriptive about how it is not working? What results are you getting?
Was This Post Helpful? 0
  • +
  • -

#9 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: How i read text between specific begin & end tags?

Posted 02 August 2009 - 05:53 PM

Quote

I want to use .rtf because i need to save color etc. information, BUT if you know better way to do this

I am also confused, because I see that you say you "want to use .rtf" but it doesn't appear that you are looking for rtf codes. Unless you're parsing html into rtf or something.
Try setting breakpoints maybe.
Was This Post Helpful? 0
  • +
  • -

#10 keksinnot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: How i read text between specific begin & end tags?

Posted 09 August 2009 - 04:41 AM

View PostLoveIsNull, on 2 Aug, 2009 - 04:53 PM, said:

Quote

I want to use .rtf because i need to save color etc. information, BUT if you know better way to do this

I am also confused, because I see that you say you "want to use .rtf" but it doesn't appear that you are looking for rtf codes. Unless you're parsing html into rtf or something.
Try setting breakpoints maybe.




1 I have one rtf document.
2 In that document there are code tags before and after some text, like this:
<text1>
To write Latin, the Romans used the Latin alphabet, derived from the Old Italic alphabet, which itself was derived from the Greek alphabet. The Latin alphabet flourishes today as the writing system for the Romance, Celtic, Germanic (including English), and some Slavic (such as Polish) languages, among others.
</text1>
<text2>
The expansion of the Roman Empire spread Latin throughout Europe, and, eventually, Vulgar Latin began to diverge into various dialects. Vulgar Latin gradually evolved into a number of distinct Romance languages by the 9th century. These were, for many centuries, only oral languages, Latin still being used for writing.
</text2>

3 I want to get for example only the text (and formatting) between tags <text1> & </text1>, AND i also want to hide the code tags so user does not see them.

The document in which the code tags are is huge.

What kind of program it will be?

It will be book reader, with ability to "turn pages" and jump between pages + bookmarks fast.
B)

Now the code i am using is still exactly same one as in the beginning, no success this far. :zzz:


In php it goes like this:
preg_match("@\{text1\}(.+?)\{/text1\}@is", $input, $matches);
$bodyText = $matches[1] ? $matches[1] : "";

Is it even possible in VB.NET?
Or is there anyone who actually knows how to use RegEx in VB.NET?

This post has been edited by keksinnot: 09 August 2009 - 04:56 AM

Was This Post Helpful? 0
  • +
  • -

#11 keksinnot  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 14-June 09

Re: How i read text between specific begin & end tags?

Posted 09 August 2009 - 08:18 AM

View Postkeksinnot, on 9 Aug, 2009 - 03:41 AM, said:

View PostLoveIsNull, on 2 Aug, 2009 - 04:53 PM, said:

Quote

I want to use .rtf because i need to save color etc. information, BUT if you know better way to do this

I am also confused, because I see that you say you "want to use .rtf" but it doesn't appear that you are looking for rtf codes. Unless you're parsing html into rtf or something.
Try setting breakpoints maybe.




1 I have one rtf document.
2 In that document there are code tags before and after some text, like this:
<text1>
To write Latin, the Romans used the Latin alphabet, derived from the Old Italic alphabet, which itself was derived from the Greek alphabet. The Latin alphabet flourishes today as the writing system for the Romance, Celtic, Germanic (including English), and some Slavic (such as Polish) languages, among others.
</text1>
<text2>
The expansion of the Roman Empire spread Latin throughout Europe, and, eventually, Vulgar Latin began to diverge into various dialects. Vulgar Latin gradually evolved into a number of distinct Romance languages by the 9th century. These were, for many centuries, only oral languages, Latin still being used for writing.
</text2>

3 I want to get for example only the text (and formatting) between tags <text1> & </text1>, AND i also want to hide the code tags so user does not see them.

The document in which the code tags are is huge.

What kind of program it will be?

It will be book reader, with ability to "turn pages" and jump between pages + bookmarks fast.
B)

Now the code i am using is still exactly same one as in the beginning, no success this far. :zzz:


In php it goes like this:
preg_match("@\{text1\}(.+?)\{/text1\}@is", $input, $matches);
$bodyText = $matches[1] ? $matches[1] : "";

Is it even possible in VB.NET?
Or is there anyone who actually knows how to use RegEx in VB.NET?



OK, OK, i figured out why those codes were not working:
I AM USING WINDOWS 7, AND IT JUST WONT DISPLAY THE CONSOLE AT ALL!


Now i need to get the results into richtextbox, how?
Was This Post Helpful? 0
  • +
  • -

#12 soltec  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-August 09

Re: How i read text between specific begin & end tags?

Posted 14 August 2009 - 09:20 PM

View Postkeksinnot, on 1 Aug, 2009 - 12:15 PM, said:

I want to read ALL text between specific <b1> and </b1> tags + hide the tags, example text:

<b1>
As part of the Visual Studio product range, Microsoft created a set of free development environments for hobbyists and novices, the Visual Studio 2005 Express series. One edition in the series is Visual Basic 2005 Express Edition, which was succeeded by Visual Basic 2008 Express Edition in the 2008 edition of Visual Studio Express.[8]

The Express Editions are targeted specifically for people learning a language. They have a streamlined version of the user interface, and lack more advanced features of the standard versions. On the other hand, Visual Basic 2005 Express Edition does contain the Visual Basic 6.0 converter, so it is a way to evaluate feasibility of conversion from older versions of Visual Basic.
</b1>


I have tried the following, but i cant figure out what's wrong. I know it isn't impossible.
Dim inputString As String = RichTextBox1.Text
		Dim regex As New Regex("[<]b1[>]""[<]/b1[>]"(RegexOptions.Compiled))
		Dim col As MatchCollection = Regex.Matches(inputString)
		For Each match As Match In col
			Console.WriteLine("[<]b1[>]" + match.Groups("[<]b1[>]").Value)
		Next



I want to use .rtf because i need to save color etc. information, BUT if you know better way to do this; please tell me. B)


Thanks for your help, it's appreciated.


You need to use the StreamReader and StreamWriter .NET functions for your textbox
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1