473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

VB.net string help

CroCrew
564 Expert 512MB
Hello Everyone,

First off I would like to thank anyone that helps me out with this problem that I have.

Ok, the environment I am working in is VB.net

I have a string that I am reading in from a file. There are “chunks” of data within the string that I want to keep and the rest of the data I could care less about. Here is a small example of the string:

xString = “434623[73899]256[346]37856[3634][367][8922] 45745[12954]35478”

The “chunks” that I am interested in are within the brackets “[###]”. What I would like to do is build an array something like this:

yString(0) = 73899
yString(1) = 346
yString(2) = 3634
yString(3) = 367
yString(4) = 8922
yString(4) = 12954

Anyone have a fast way of doing this? Examples?

Again; Thanks for all your help,
CroCrew~
Feb 4 '10 #1

✓ answered by lonekorean

Not a problem. Replace the "\d+" with ".+?"

Here's the modified regex in action: find anything enclosed in brackets

To explain, \d matches any digit, and the + means "one or more". So \d+ matches any series of digits, AKA a number.

In the new pattern, the . matches anything (except newlines), + means the same thing (one or more) and the "?" in this context means "but match as few as possible". The ? is important because it keeps the pattern from being "greedy" and going beyond the closing bracket. If you want to see what I mean by being greedy, take it out on that testing page, and you'll see what I mean.

11 1902
tlhintoq
3,525 Expert 2GB
I'm sure someone will have a way of doing this with RegEx, but I'm not good with RegEx.

Me... I'm more brute force. Use a 'for' loop to look at each character. If it is a '[' note the position... keep looping until you find a ']' and note the position. Your desired substring is between the two positions you just found. Keep going to the end of the string.
Feb 4 '10 #2
Well, here's the regex way. :)

Try this regex pattern: (?<=\[)\d+(?=\])

The numbers you want will be in the collection of matches you get from the result.

You can see it in action here: http://regexstorm.net/Tester.aspx?p=...b12954%5d35478
Feb 4 '10 #3
tlhintoq
3,525 Expert 2GB
I'm learning too here...

The most complex RegEx I've ever done is things like is it an alpha, is it positive and so on. The values are evaluated within the function and return a result

It seems every example I run across is about matching or validating. They all seem to get boolean results.

Expand|Select|Wrap|Line Numbers
  1. // Function to test for Positive Integers with zero inclusive
  2. public static bool IsWholeNumber(String strNumber)
  3. {
  4.      Regex objNotWholePattern = new Regex("[^0-9]");
  5.      return !objNotWholePattern.IsMatch(strNumber);
  6. }
How would one use the RegEx you supplied to get a string[] returned, as in the OP's need/example?

Expand|Select|Wrap|Line Numbers
  1. string xString = “434623[73899]256[346]37856[3634][367][8922]45745[12954]35478”;
  2. Regex BetweentTheBrackets =  new Regex(?<=\[)\d+(?=\]);
  3. string[] Results = ???? What goes here? How to format the use?
Feb 5 '10 #4
tlhintoq
3,525 Expert 2GB
I'm not trying to hijack this thread, just work out a complete function that the OP could use.

So I tried to do some research and figure out the answer to my own question.
That's what we do here, right? As I always advise others I started with MSDN and found this helpful example:
http://msdn.microsoft.com/en-us/libr...66(VS.71).aspx

That lead me to build this function
Expand|Select|Wrap|Line Numbers
  1.         // Function to return a string[] from a source string and Regex pather
  2.         public static List<string> FindWithin(string SourceString, string Pattern)
  3.         {
  4.             Match m;
  5.             CaptureCollection cc;
  6.             GroupCollection gc;
  7.             List<string> Results = new List<string>();
  8.  
  9.             Regex r = new Regex(Pattern);
  10.             // Define the string to search.
  11.             m = r.Match(SourceString);
  12.             gc = m.Groups;
  13.  
  14.             // Loop through each group.
  15.             for (int i = 0; i < gc.Count; i++)
  16.             {
  17.                 cc = gc[i].Captures;// So this is still an array of sorts
  18.  
  19.                 // Loop through each capture in group.
  20.                 for (int ii = 0; ii < cc.Count; ii++)
  21.                 {
  22.                     Results.Add(cc[ii].ToString());
  23.                 }
  24.             }
  25.             return Results;
  26.         }
  27.  
and then call it like so
Expand|Select|Wrap|Line Numbers
  1.         private void regexToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             List<string> SomeStrings =
  4.             FindWithin("434623[73899]256[346]37856[3634][367][8922]45745[12954]35478",
  5.                                          @"(?<=\[)\d+(?=\])+");
  6.             int count = SomeStrings.Count;
  7.  
  8.         }
  9.  
But it seems no matter how I call it (with or without the trailing plus which I thought might be for all matches not just the first)... The gc collection still only matches to the first bracketed set of numbers [78399], not all of them.

So what am I as a Regex newbie missing?
Feb 5 '10 #5
No worries, here's a code snippet for you:
Expand|Select|Wrap|Line Numbers
  1.         Regex r = new Regex(@"(?<=\[)\d+(?=\])");
  2.         MatchCollection mc = r.Matches("434623[73899]256[346]37856[3634][367][8922] 45745[12954]35478");
  3.         foreach (Match m in mc)
  4.         {
  5.             string s = m.Value;
  6.         }
  7.  
Basically, we instantiate a new Regex object with the pattern we want to run. The Regex object has a method Matches() on it that takes in the body of text to run the pattern on, and returns the collection of matches (these are the results we want). For each such match in this collection, we can look at the Value property to get the actual string value we want.

In this particular case, you don't need to worry about groups or captures. We can get what we need just from looking at matches.

Hope that helps. :)
Feb 5 '10 #6
tlhintoq
3,525 Expert 2GB
Thank you for that fine explanation. That allowed me to correct the function to:
Expand|Select|Wrap|Line Numbers
  1.         // Function to return a string[] from a source string and Regex pather
  2.         public static List<string> FindWithin(string SourceString, string Pattern)
  3.         {
  4.             List<string> Results = new List<string>();
  5.             Regex r = new Regex(Pattern);
  6.             MatchCollection mc = r.Matches(SourceString);
  7.             foreach (Match m in mc)
  8.             {
  9.                 Results.Add(m.Value);
  10.             }
  11.             return Results;
  12.         }
  13.  
being called from a testing menu option like
Expand|Select|Wrap|Line Numbers
  1.         private void regexToolStripMenuItem_Click(object sender, EventArgs e)
  2.         {
  3.             List<string> SomeStrings =
  4.                 Saint.StRegEx.FindWithin("434623[73899]256[346]37856[3634][367][8922]45745[12954]35478",
  5.                                          @"(?<=\[)\d+(?=\])");
  6.             int count = SomeStrings.Count;
  7.  
  8.         }
  9.  
Very efficient and reusable. Thanks for holding the hands of a couple newbies. I see myself taking a lot of time to learn Regex for future projects.
Feb 5 '10 #7
No problem, happy to help. :)

Pardon the self-promotion, but I have a site you might find useful for your future regex endeavors: Regex Storm.

It runs on the .NET regex engine, and I tried to cater it to programmers like us that need to come up with regex to use in our code.
Feb 5 '10 #8
CroCrew
564 Expert 512MB
Thanks for all the help guys!!
Feb 5 '10 #9
CroCrew
564 Expert 512MB
I am not good working with RegEx. I just found out that the data could contain “any” characters within the brackets. Well, one good thing is that there can be a bracket within a bracket so I have that going for me </grin>.

Here is another small example of some data:

xString = “dd#[AN$$970]##[DaD]134[(fff4)][ttt][111] *j^333[323]ff-32”

What would I use as my expression to capture “any” characters?

Again; thanks for all the help,
CroCrew~
Feb 5 '10 #10
Not a problem. Replace the "\d+" with ".+?"

Here's the modified regex in action: find anything enclosed in brackets

To explain, \d matches any digit, and the + means "one or more". So \d+ matches any series of digits, AKA a number.

In the new pattern, the . matches anything (except newlines), + means the same thing (one or more) and the "?" in this context means "but match as few as possible". The ? is important because it keeps the pattern from being "greedy" and going beyond the closing bracket. If you want to see what I mean by being greedy, take it out on that testing page, and you'll see what I mean.
Feb 5 '10 #11
CroCrew
564 Expert 512MB
Thanks tlhintoq & lonekorean! for all your help!
Feb 5 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Helmut Jarausch | last post by:
Hi, entering help('rstrip') or help('ljust') into IDLE's shell window I only get no Python documentation found ...
3
by: Imran Aziz | last post by:
Hello All, I am getting the following error on our production server, and I dont get the same error on the development box. Unable to cast object of type 'System.Byte' to type 'System.String'. ...
5
by: Sia Jai Sung | last post by:
Hi, I have a class that I modify from a sample program, like below ========================================== Imports System Imports System.Web.UI Imports System.Security.Cryptography ...
6
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or...
6
by: Calros Lo | last post by:
Dear all: I develop a programe that need when I get a string , such as "123" or "ABC",if I get string "123" and the system will help me to create new string "124" , if I get string "ABC" and the...
5
by: Joe Nova | last post by:
I'm a C++ noob and I need a little help manipulating strings. I've got a program that takes an expression in the form: "operand1 operator operand2" I'd like to: 1. Find the total length...
9
by: MikeB | last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project for school and one of the requirements is that every screen should have a "Help" button. I could do it by writing a clumsy...
8
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to...
13
by: mac | last post by:
Hi, I'm trying to write a fibonacci recursive function that will return the fibonacci string separated by comma. The problem sounds like this: ------------- Write a recursive function that...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.