Re: Regular Expressions?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Sorry, forgot to context switch!

Here is the code in VB

'
' Create our regex
'
dim r as new Regex("\[(?<Number>.*?)\]")
'
' Setup our capture string
'
dim test as string = "[54x454],[22b6],[885333]"
'
' Get our matches
'
dim m as MatchCollection = r.Matches(test)
'
' Do with them as we will!
'
for i as integer = 0 to m.Count-1
Console.WriteLine(m(i).Groups(1).Value)
next


On Thu, 23 Nov 2006 20:56:46 +0300, "Rad [Visual C# MVP]"
<nospam@xxxxxxxxxx> wrote:

Hey G,

Try this code:

//
// Create our regex
//
Regex r = new Regex(@"\[(?<Number>.*?)\]");
//
// Setup our capture string
//
string test = @"[54x454],[22b6],[885333]";
//
// Get our matches
//
MatchCollection m = r.Matches(test);
//
// Do with them as we will!
//
for(int i = 0; i<m.Count;i++)
Console.WriteLine(m[i].Groups[1].Value);

On Thu, 23 Nov 2006 12:00:19 -0000, "G .Net" <nodamnspam@xxxxxxxxx>
wrote:

Thanks guys

Looks promising!

Could I bother you again. What if the characters I'm interested are enclosed
in [ and ]. For example,

"[54x454],[22b6],[885333]"

G

"Ken Tucker [MVP]" <vb2ae@xxxxxxxxxxxxx> wrote in message
news:C6B15518-0E76-4900-8671-789EC607E4C4@xxxxxxxxxxxxxxxx
Dim strTest As String = "#54x454#,#22b6#,#885333#"
Dim strNums() As String
strNums = strTest.Replace("#"c, "").Split(","c)

For Each s As String In strNums
Debug.Print(s)
Next

Ken
---------------------------
"G .Net" <nodamnspam@xxxxxxxxx> wrote in message
news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@xxxxxxxxxxxx
Hi

I was wondering if you could help me with the following:

I have a string e.g. "#54x454#,#22b6#,#885333#".

I want to be able to store in an array list, the following from the
string:

54x454
22b6
885333

That is, the items between the hash signs.

I believe this could be done with regular expressions?

Can anybody show me some code to achieve this. By the way, there may be
more than three items.

Thanks in advance

G



--

Bits.Bytes.
http://bytes.thinkersroom.com
.



Relevant Pages

  • Re: Fastest way to search a string for the occurance of a word??
    ... but the OP's question was what's the "Fastest way to search a string ... in all the tests I did here, the Regex was by far superior. ... However, of course, if you've got new regular expressions all ... Sure - but just that extra Match object could be relevant if the search ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regular expression help
    ... Basically because if you remove everything that is optional in the regex below you end up with an empty regex: ... So the regex engine will try to match on every character in the string: ... , comma doesn't match, but the nothingness in front of it does. ... A quote followed by any sequence of characters that is not a quote, ...
    (microsoft.public.dotnet.framework)
  • Re: Regex optimization
    ... I was hoping that someone with knowledge of the Regex engine could ... match per string for either Regex. ... reluctant modifier, may be slower .*?, +? ... Variable parts will try to capture as much as possible. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Regex Capture problem
    ... "learned" my regex using a freeware utility that had slightly different ... was trying to capture instead of. ... I have used Regex utilities before, so I understand the concepts of text ... Function RESub(str As String, SrchFor As String, ReplWith As String) As String ...
    (microsoft.public.excel.programming)
  • Re: Trim a multiple line message to a single line
    ... You can do this quite easily with either a regex or a simple function I'll try to demonstrate both: ... private string LayoutInput ... Could you send a sample file with two of these data blocks f what ...
    (microsoft.public.dotnet.languages.csharp)