473,500 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
clue.

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) &
"javascript:jsOpen[\s\S]*"

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 1639
Lisa

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

JimT
Nov 20 '05 #2
Lisa

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

Jim

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
Regex("^(?<MatchedStringName>\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a
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
href="javascript:jsOpen
End Sub
End Module

For detailed information about group and regular expression, you may take a
look at the link below.
Grouping Constructs
http://msdn.microsoft.com/library/de...us/cpgenref/ht
ml/cpcongroupingconstructs.asp
Regular Expression Language Elements
http://msdn.microsoft.com/library/de...us/cpgenref/ht
ml/cpconregularexpressionslanguageelements.asp

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

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

2
2309
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...
1
1682
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...
2
1619
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),...
1
266
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 =...
3
1422
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,...
17
2761
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...
5
3775
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>(+)+),...
0
6171
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...
7
4387
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...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5490
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4611
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
672
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.