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
Solved

RegEx issue

Posted on 2004-08-13
6
183 Views
Last Modified: 2010-04-15
ok, so I have a class which I've created that inherits from the class Regex [code shown below] and so far the expression has worked well for most cases except one.

Heres what its supposed to do:  Looks through any input tag (eg. <input type="text"...>) and find the attribute I specify followed by its value which I would like stored in the submatch.  In cases where I have a quoted value (tpe="text"), everything works fine... where it flunks is when the attribute has no quotes (sigle or double quotes, eg. type=TEXT).

Can anyone see what I'm doing wrong here?

protected class AttributeExpression : Regex
{
private static readonly RegexOptions PreDefOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled;

public AttributeExpression(string attributeName) : base(" " + attributeName + "=\"([^\"]+)\"| " + attributeName + "='([^']+)'| " + attributeName + "=([^\\s]+)[\\s]", PreDefOptions){}
}
0
Comment
Question by:yleviel
  • 2
  • 2
  • 2
6 Comments
 
LVL 2

Expert Comment

by:davidastle
ID: 11797986
Your problem is with the last section,
attributeName + "=([^\\s]+)[\\s]",

What your first group in this snippet, ([^\\s]+), does is seach for one or more non white space characters.  Therefore, it will only stop when you get to a white space.  After that, it tries to match [\\s], which is also looking for a non white space.  So you get to a white space, and try to match it with a non white space, and your match fails.
0
 
LVL 19

Accepted Solution

by:
drichards earned 120 total points
ID: 11798924
No, the final [\s] looks FOR whitespace.

When I test your last expression it works - almost.  If the input looks like this:

    <input text=TEXT>

then the '>' is included in the capture.  I changed to this:

<att name>=([^>\s]+)[>\s]

Seems to work.  What was your test text?  Mine was dirt simple:

"<html><head><title>My Doc</title></head><body><form><input text=myText></input><input text='Some Text'></input></form></body></html>"

Your class with my small mod picked out both text= attributes and correctly captured "myText" and "Some Text".  I also found it a bit easier to name the groups:
----------------------------------------
protected class AttributeExpression : Regex
{
private static readonly RegexOptions PreDefOptions = RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled;

public AttributeExpression(string attributeName) : base(" " + attributeName + "=\"(?<val>[^\"]+)\"| " + attributeName + "='(?<val>[^']+)'| " + attributeName + "=(?<val>[^>\\s]+)[>\\s]", PreDefOptions){}
}
0
 
LVL 2

Expert Comment

by:davidastle
ID: 11799092
Oops, sorry
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Author Comment

by:yleviel
ID: 11811467
drichards,

The regex you made works almost flawlessly... the only case where I've had problems is when the case of value="" shows up.  When this happens, the submatch returns "\"\"" (as in two quote symbols).  Anything you can think of to remedy this issue?

Thanks!
0
 
LVL 2

Author Comment

by:yleviel
ID: 11811640
ok, I changed the expression to handle zero or more chars in the quotes. and this has worked for all my test cases.  If you see nothing wrong with the new expression I'll award you the points.

public AttributeExpression(string attributeName) : base(" " + attributeName + "=\"(?<val>[^\"]*)\"| " + attributeName + "='(?<val>[^']+)'| " + attributeName + "=(?<val>[^>\\s]+)[>\\s]", PreDefOptions){}
0
 
LVL 19

Expert Comment

by:drichards
ID: 11812457
Just that you'll probably want the same change in the single quote expression (zero or more instead or 1 or more) and make sure there are no other terminal cases in the no-quote expression (anything other than whitespace and '>' that would end the match?).
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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