Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello Extreme.Net Admin and Members.

 

Primary Thanx

 

Firstly i must say what a great forum you got here, seems very friendly :) as i am a "new" user to programming on the whole i feel confident enough to ask you guys a few questions if you dont mind. Maximum Appreciation to you all, ill continue with my problem.

 

After reading through these forums over the last week or so, ive been able to "Succesfully" create myself a few simple but effective apps for my learning process. I am now at the stage of needing a little more help and pointers in the right direction from you great people.

 

What im trying to accomplish.

 

Reading a File(text) for certain values to place in a text Slider/Box for changing , then the ability to "save" that file

My "only" knowledge at this point, is i can Read a "c:/mytext.txt" and get all of the data into a text box.

 

My Example mytext.txt

I need to know how to Look for certain Values_The Numbers_ In this file

Speed 2132

Cutsd 32.1

SkankStr 0.23.21

 

Then display those values, maybe in a Box with a Slider to Control Min-Max Values > Then save back to file

 

 

I hope i have explained everything above as all my knowlede and learning of VB so far(.NET) has come from "this" forum, so thank you all very much so far, very much appreciated. I look forward to any replies.

 

 

Cecil B

UK England

  • *Experts*
Posted

You could read the whole file into a string, then if you are looking for numbers use Regular Expressions to find any numeric values.

First read the file using a IO.StreamReader's read to end method and then use a RegularExpression object to seach the string. Ad then you could use a pattern like: "[0-9]" to search for numbers using Regular Expressions.

Posted

Thx for the reply. I am currently starting with IO.StreamReader

 

 

:)

Dim f As IO.File

Dim sr As IO.StreamReader = New IO.StreamReader(New IO.FileStream("C:\mytext.txt", IO.FileMode.Open))

TextBox1.AppendText(sr.ReadToEnd)

sr.Close()

:)

 

I`m not familiar with the RegularExpression Object, , ive tried to add the Import System.Text.RegularExpressions at the top of my code like some things ive read suggeested but it fails to even take that line ? as IMports is underlined as error i think.

 

Any examples of use would be great.

Thx for help, im sure ill be around for a while yet :) but im more than willing.

  • *Experts*
Posted (edited)

This should work:

Imports System.Text.RegularExpressions

Now create a new RegularExpression object with the following pattern:

Dim reg As New Regex("[0-9]+")

This pattern will match any integer number, like: 1234, or 123. Now to get all the matches from that pattern, do this:

Dim matchcoll As MatchCollection 'all the matches regex finds in the string
matchcoll = reg.Matches("text to check") 'this returns all matches from the regex

Now you can go through all of the matches like this:

Dim m As Match
For each m in matchcoll
   MessageBox.Show(m.Value)
next 

This example will show you all the matches in a MessageBox.

:)

Edited by mutant
Posted

Hi Mutant,

 

My 1st problem is when i add :

 

Imports System.Text.RegularExpressions

 

The above Imports is underlined as an error ? when i goto add Reference, i dont see the .text* for me to add. Bare with me please i am obviously doing /adding it wrong ?

 

Any clues. do i add it manually ? or by selective click somewhere i dont know.

 

Thanx

  • *Experts*
Posted

You dont have to add a reference to the Text namespace, dont have to do it for RegularExpressions one either because they are included in the same physical file as System reference which should be already referenced.

Where are you putting the Imports statement? What is the error?

Posted

I was putting Imports System.Text.RegularExpressions in the 1st public bit, wrong move figured that 1 out, needed to be at very very top of code.

 

On insertion of your code "below my streamreader stuff ??" error occurs at :

 

matchcoll = **reg.Matches** (without the **) :)

 

:eek:

  • *Experts*
Posted

Im sorry, it should it be like this:

matchcoll = reg.Matches(variablewithtext)

Just put the name of the variable you have the text in when caling Matches. Sorry, I forgot to type that little piece of code :)

Posted

Thanks.

 

When i press my "Button" it now displays the value i place in the () i.e

 

matchcoll = reg.Matches(1)

Would display 1 in the message box.

 

You said

Just put the name of the variable you have the text in when caling Matches. code :) [/b]

 

? sry lost there. Also how would i now associate that to look in mytext.txt ?

 

Should i be using my 1st above piece of code for file open/ Stream Reader.

  • *Experts*
Posted

In the case of the code you showed eariler you would do this:

matchcoll = reg.Matches(TextBox1.Text)

Sorry I wasnt clear, you have pass in a String type to the Matches().

  • *Experts*
Posted
Here is an example file, I dont know what VS.NET version you are using so I didnt attach the project. Create a new windows application project and then just add this file to it and run it.

regex.vb

Posted (edited)

Thats Great, absoloute perfection Sir ;)

 

I just incorporated it into mytext.txt and realised how to do that bit, I will have a good mess now as you have certainly got me on 1 now :). I got all values in my txt to display 1 after the other in the message box :). I will try an get those values to display in the several box`s i got setup.

 

Gonna grab some coffee and then start to venture further in my task.

 

Thanx Mutant, I`m sure there will be a few questions, Thank you Very much.

Edited by CecilB
  • 2 weeks later...
Posted

Sry, was dbl posted for strange reason. edited this 1

 

here is example which i forgot to include in above post

 

Example same as above

 

Speed 2132

Cutsd 32.1

SkankStr 0.23.21

 

and read only the numbers...

Posted

Back after Coffee, now have problems on getting a specific value like "2132" from "Speed 2132" and so on, and display it in a box/slider/whatever.

 

Many thanx

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...