473,382 Members | 1,619 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,382 software developers and data experts.

Desperate for some help with regular expression pattern

I am trying to take a string and parse it out into multiple strings
based on a pattern but am stuck and am hoping someone can give me a

My pattern looks like so: sMatch =
"\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a class=link href=" & Chr(34) &

What I want is to take a single string and for anything that starts
with a date/time and "<a class=link href=" & Chr(34) &
"javascript:jsOpen", divide the single string into multiple strings.
The trouble seems to be the last part of the pattern "[\s\S]*".
Anything can appear after the first part of the string including
linefeeds, tabs, form-feeds, etc. The above pattern gives me the
whole string instead of several as I expected.

I have tried various combinations of Singleline and Multiline options.

sMatch = "^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a class=link href=" &
Chr(34) & "javascript:jsOpen)(.*)$" - gives me the whole string.
sMatch = "^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a class=link href=" &
Chr(34) & "javascript:jsOpen)[\s|\S]*" - gives me the whole string
sMatch = "^(\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a class=link href=" &
Chr(34) & "javascript:jsOpen).*" - gives me several strings but those
strings end when the first chr(10) is encountered after the first part
of the pattern.

I have tried the RegeEx.Split where I split by patterns similiar to
above and this gets me closer but then I lose the first part of the
string that matches the pattern.

Any ideas or advice would be appreciated.
Nov 20 '05 #1
3 1630

There are a number of ways to make this work. All are explained in Jeff Friedl's book, "Mastering Regular Expressions" from O'Reilly

If I were doing this, I would either use "regex.matches" and see if the matches collection had collected more than one group, or "regex.match" followed by "regex.replace" repeatedly to find the first hit, replace it with something the regex won't catch, and try to find another match in the same input. When m.success = false you can stop looking

There is probably a real slick way to do what you want, but I've barely finished reading the book myself

By the way, there is an excellent section in the back of the book about using regexes in .NE

Hope this helps

Nov 20 '05 #2

If you paste the following into a "button_click" subroutine it will pop up a message box that says "found 3 of 'em" when you click on the button. It looks pretty messy here, but pasting it into a vb program should straighten it out. If it doesn't, everything should be on a line by itself except the string s, which is three lines long

Once you have the match collection, you can extract a string index for each instance which tells you where your search string begins in the target string. Then s.substring(index, ...) will peel out the piece you want

If this is what you need, wonderful! If not, send me an e-mail and we can try to figure it out together

Good Luck


Dim r As New Regex("\d{4}\-\d{2}\s\d{2}:\d{2}\sby\s<a class=link href=" & Chr(34) & """javascript:jsOpen"""
Dim mc As MatchCollectio
Dim s As String = "2004-23 21:04 by <a class=link href=" & Chr(34) & """javascript:jsOpen""" & " stuff " &
"2002-04 08:59 by <a class=link href=" & Chr(34) & """javascript:jsOpen""" & " nonsense " &
"2001-99 12:00 by <a class=link href=" & Chr(34) & """javascript:jsOpen""" & " garbage
mc = r.Matches(s
If mc.Count > 0 The
MsgBox("Found " & Format(mc.Count, "D") & " of 'em"
End I

Nov 20 '05 #3
Hi Lisa,

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you wants to use a regular
expression to exact a matched string from a long string.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

I think you may try to use the group to do the job.

Imports System.Text.RegularExpressions
Module Module2
Public Sub Main()
Dim Text As String = "2004-04-12 09:31 by <a class=link
href=""javascript:jsOpen fsafsadfs"
System.Console.WriteLine("text=[" & Text & "]")
Dim r As Regex = New
class=link href=""javascript:jsOpen)(.*)", RegexOptions.IgnoreCase)
Dim mt As Match = r.Match(Text)
Console.WriteLine(mt.Groups("MatchedStringName").T oString())
'The Line will print out 2004-04-12 09:31 by <a class=link
End Sub
End Module

For detailed information about group and regular expression, you may take a
look at the link below.
Grouping Constructs
Regular Expression Language Elements

Please apply my suggestion above and let me know if it helps resolve your

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
by: Jeff | last post by:
For the life of me, I can't create a working regular expression for a schema pattern to do the following: Validates if an entire word does NOT match the entire word in the pattern. For...
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped),...
by: Lisa Bogart | last post by:
I am trying to take a string and parse it out into multiple strings based on a pattern but am stuck and am hoping someone can give me a clue. My pattern looks like so: sMatch =...
by: rodchar | last post by:
hey all, what would my expression look like if i wanted to make sure that the input matched the following pattern. c:\filename.ext it doesn't have to be the c drive just a letter, colon,...
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
by: shawnmkramer | last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just hanging and eventually getting a message "Requested Service not found"? I have the following pattern: ^(?<OrgCity>(+)+),...
by: altavim | last post by:
Usually when you make regular expression to extract text you are starting from simple expression. When you got to know target text, you are extending your expression. Subsequently very hard to ready...
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start...
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: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.