473,563 Members | 2,653 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:jsO pen[\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:jsO pen", 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:jsO pen)(.*)$" - 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:jsO pen)[\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:jsO pen).*" - 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 1656
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.matc hes" and see if the matches collection had collected more than one group, or "regex.matc h" followed by "regex.repl ace" 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_cli ck" 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(ind ex, ...) 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:j sOpen"""
Dim mc As MatchCollectio
Dim s As String = "2004-23 21:04 by <a class=link href=" & Chr(34) & """javascript:j sOpen""" & " stuff " &
"2002-04 08:59 by <a class=link href=" & Chr(34) & """javascript:j sOpen""" & " nonsense " &
"2001-99 12:00 by <a class=link href=" & Chr(34) & """javascript:j sOpen""" & " 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.Reg ularExpressions
Module Module2
Public Sub Main()
Dim Text As String = "2004-04-12 09:31 by <a class=link
href=""javascri pt:jsOpen fsafsadfs"
System.Console. WriteLine("text =[" & Text & "]")
Dim r As Regex = New
Regex("^(?<Matc hedStringName>\ d\d\d\d-\d\d-\d\d\s\d\d:\d\d \sby\s<a
class=link href=""javascri pt:jsOpen)(.*)" , RegexOptions.Ig noreCase)
Dim mt As Match = r.Match(Text)
Console.WriteLi ne(mt.Groups("M atchedStringNam e").ToString ())
'The Line will print out 2004-04-12 09:31 by <a class=link
href="javascrip t: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/cpcongroupingco nstructs.asp
Regular Expression Language Elements
http://msdn.microsoft.com/library/de...us/cpgenref/ht
ml/cpconregularexp ressionslanguag eelements.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
2316
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 advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
1
1688
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 example, in the pattern the word my be "foo". If the word "foo" is in my XML it should fail. If any other word, such as "cat" or "foot" or "barfoo" is in...
2
1626
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), RegexOptions.CultureInvariant ); string s = re.Replace( input, "" ); It doesn't seem to work, regular expression return without filter out any...
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 = "\d\d\d\d-\d\d-\d\d\s\d\d:\d\d\sby\s<a class=link href=" & Chr(34) & "javascript:jsOpen*" What I want is to take a single string and for anything that starts...
3
1434
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, filename, and extension.
17
2770
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 might be a neat way to solve this, but I am new to them. Can anyone give me a hint here? The catch is, it must only find tokens that are not quoted...
5
3780
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>(+)+), City of, (?<OrgState>(()|( +\.)))( \((?<OrgCountry>{2,})\))?$ (ignore the line wrap)
0
6178
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 long set of special symbols and impossible to improve such expression. We have to create ’smart’ regular expression. Instead of write one line...
7
4390
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 and stop codons on either side. This is simple enough... for example: start = AUG stop=AGG BBBBBBAUGWWWWWWAGGBBBBBB
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7885
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
923
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.