Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

regular expression not working

Posted on 2009-07-13
11
Medium Priority
?
315 Views
Last Modified: 2013-11-07
Hi

I have a reg ex that is not working and i can't see for the life of me why. I have copied the text I am trying to parse into the code window. I think it will be apparent from my reg ex that I am trying to extract the exception type, its message, the target site and the strack trace.

Here is my code
errorMessage.Replace("\n", "").Replace("\r", "")
Dim regExString As String = ".*Type\s:\s(.*)Message\s:\s(.*)Source.*TargetSite\s:\s(.*)Stack Trace\s:\s(.*)"

Dim regEx As Regex = New Regex(regExString)
 Dim m As Match
m = regEx.Match(message)
if m.success

The reg ex fails. I have been trying to break it down bit by bit. This works
Dim regExString As String = ".*Type\s:\s(.*)"
However the string that is matched is only this :
System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

This is confusing me. I would have thought this reg ex would match everything after 'Type : ' but for some reason it does stop where i want it to without me telling it to.

This fails  Dim regExString As String = ".*Type\s:\s(.*)Message"

I really cant see why. Any help much appreciated.
thanks
HandlingInstanceID: d62db6a9-a539-4d6f-95e4-8efa1f6295c4
An exception of type 'System.ArgumentException' occurred and was caught.
------------------------------------------------------------------------
07/13/2009 11:09:27
Type : System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Parameter 'p_JourneyID' not found in the collection.
Source : EntitySpaces.Interfaces
Help link : 
ParamName : 
Data : System.Collections.ListDictionaryInternal
TargetSite : EntitySpaces.Interfaces.esDataResponse ExecuteScalar(EntitySpaces.Interfaces.esDataRequest, EntitySpaces.Interfaces.esProviderSignature)
Stack Trace :    at EntitySpaces.Interfaces.esDataProvider.ExecuteScalar(esDataRequest request, esProviderSignature sig)
   at EntitySpaces.Core.esEntityCollection.ExecuteScalar(esQueryType queryType, String query, esParameters parms)
   at EntitySpaces.Core.esUtility.ExecuteScalar(esQueryType queryType, String query, esParameters parms)
   at SureTrack2.Server.TCPClientThread.Connect() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SureTrack2.Server\SureTrack2.Server\TCPClientThread.vb:line 480
 
Additional Info:
 
MachineName : OSCAR
TimeStamp : 13/07/2009 10:09:27
FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
AppDomainName : SureTrack2.Server.vshost.exe
ThreadIdentity : 
WindowsIdentity : OSCAR\Administrat

Open in new window

0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
11 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 24839507
Can you try the following? You may not have the single-line option enabled. The dot (.) can either match or break at newline characters--depending on whether or not you enable single-line mode. There are two ways to do so--either inline or as a parameter to the regex constructor. Here, I have enabled it inline--(?s).
(?s).*?Type\s*\:\s*(.*?)Message\s*\:\s*(.*?)Source.*?TargetSite\s*\:\s*(.*?)Stack Trace\s*\:\s*(.*?)

Open in new window

0
 
LVL 28

Expert Comment

by:strickdd
ID: 24839526
It might make more sense to do something like:

Dim regExString As String = ".*Type\s:\s(.*)"
'Do what you need here to get the message

regExString As String = ".*Message\s:\s(.*)"
'Do what you need here to get the message
'Etc
0
 

Author Comment

by:andieje
ID: 24839703
Hi

Kaufmed,that fixed it. Please can you explain why i needed single line mode. I believe single line mode means that the . character will not stop at newline characters. That makes sense to me but i thought i had removed new line characters by doing this

errorMessage.Replace("\n", "").Replace("\r", "")

thanks
0
Basic Security of Your VPC

So, you’ve got this shiny new VPC and a fancy new application configured on your EC2 servers ready to go. This application is only accessible from your computer, which is great for security, but you need your users to be able to access it! So, what’s the easiest way to do this?

 

Author Comment

by:andieje
ID: 24839756
Hi

One more thing, the reg ex doesn't quite work because it is not picking up the stack trace at the end. The reg ex test is being passed but the stack trace group of the rex ex comes back with a string length of 0. I don't understnd why its doing that either!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24839780
See notes below.
// I believe you used the following
 
/* Calls replace on errorMessage, but does not modify errorMessage.
 *Replace returns the modified string
 */
 
errorMessage.Replace("\n", "").Replace("\r", "");
 
 
// You needed to use the following
 
/* Calls replace and stores the result back to errorMessage */
 
errorMessage = errorMessage.Replace("\n", "").Replace("\r", "")

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24839814
It appears I made an error with the last capture group. The last group currently is defined as (.*?), where the question mark makes the star (*) non-greedy. Try removing the question mark as I have done below, and you should get the proper results.
(?s).*?Type\s*\:\s*(.*?)Message\s*\:\s*(.*?)Source.*?TargetSite\s*\:\s*(.*?)Stack Trace\s*\:\s*(.*)

Open in new window

0
 

Author Comment

by:andieje
ID: 24839862
I had to put the end of line character in for some reason
       Dim regExString As String = "(?s).*?Type\s*\:\s*(.*?)Message\s*\:\s*(.*?)Source.*?TargetSite\s*\:\s*(.*?)Stack Trace\s*\:\s*(.*?)Additional Info.*?$"

no idea why
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24839907
See previous post for correction without end-of-line.

An explanation for your method:

    Because I defined the last group as (.*?), the question mark tells the star to only match the minimum that will satisfy the condition--which in your original case would have been a single space. Because you put the $, the minimum condition became "all characters until the end-of-search-string is found."
0
 

Author Comment

by:andieje
ID: 24844495
Hi

So this at the start of the expression makes vb.net treat the input as a single line and not stop at newline characters: (?s)

and a ? after the * makes it non-greedy?

many thanks
0
 

Author Closing Comment

by:andieje
ID: 31602805
thanks
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24861765
Yes.

NP. Glad to help :)
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question