|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Q: Regular Expressions?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
Show quote
Hide quote
"G .Net" <nodamnspam@email.com> wrote in message Are all of the items comma delimited and bracketed with # signs? Are there news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... > 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. leading or trailing spaces (i.e. between the commas?). If the format is strictly adhered to, something simple like this will work: Dim theString As String = "#234B#,#23324#,#23423ddd#,#23423#" Dim theItems() As String = theString.Split(","c) For i As Integer = 0 To theItems.Length - 1 theItems(i) = theItems(i).Substring(1, theItems(i).Length - 2) Next 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 --------------------------- Show quoteHide quote "G .Net" <nodamnspam@email.com> wrote in message news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... > 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 > 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 Show quoteHide quote "Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com... > 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@email.com> wrote in message > news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... >> 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 >> > You can use the trim function then I guess:
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.Trim("[", "]") Next Greets, Marcel G .Net schreef: Show quoteHide quote > 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]" <vb***@bellsouth.net> wrote in message > news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com... > > 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@email.com> wrote in message > > news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... > >> 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 > >> > > 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); Show quoteHide quote On Thu, 23 Nov 2006 12:00:19 -0000, "G .Net" <nodamnspam@email.com> Bits.Bytes.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]" <vb***@bellsouth.net> wrote in message >news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com... >> 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@email.com> wrote in message >> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... >>> 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 >>> >> > -- http://bytes.thinkersroom.com 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@nospam.com> wrote: Show quoteHide quote >Hey G, Bits.Bytes.> >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@email.com> >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]" <vb***@bellsouth.net> wrote in message >>news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com... >>> 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@email.com> wrote in message >>> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... >>>> 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 >>>> >>> >> -- http://bytes.thinkersroom.com Thanks guys for all your help. Most useful.
G Show quoteHide quote "Rad [Visual C# MVP]" <nospam@nospam.com> wrote in message news:ehpbm2pd8m9vdqutj9baqq8ch547amdb9q@4ax.com... > 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@nospam.com> 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@email.com> >>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]" <vb***@bellsouth.net> wrote in message >>>news:C6B15518-0E76-4900-8671-789EC607E4C4@microsoft.com... >>>> 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@email.com> wrote in message >>>> news:bdadneeMCM9M4_jYnZ2dnUVZ8t2dnZ2d@pipex.net... >>>>> 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
get values from asp pass to batch file !!!
A Modest Question Restart a Sub and keep loop going SQL connection problem Inherited handler problem. tell more about .net framework date time picker and validate event ( is this a bug ? ) Automatic Form Fill in Adapter Update...Syntax error embem Excel in winford using vb.net |
|||||||||||||||||||||||