?
Solved

RegEx issue

Posted on 2004-08-13
6
Medium Priority
?
187 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
[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
  • 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 480 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month8 days, 19 hours left to enroll

765 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