473,395 Members | 1,488 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,395 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
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 1632
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.