Solved

regex (get all inputs of form)

Posted on 2010-11-10
17
425 Views
Last Modified: 2012-08-14
How can I fetch the values of each input from the attached HTML using regular expression in c#.

please provide the code also and you need to explain it also in simple words. it will be much appreciated.

0
Comment
Question by:Dinesh Kumar
  • 7
  • 5
  • 2
  • +2
17 Comments
 
LVL 8

Expert Comment

by:guvera
Comment Utility
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
please provide the working code!
0
 
LVL 8

Expert Comment

by:guvera
Comment Utility
Hi,

May i know how is your input?

Because the regular expression you need to match the values and capture it.

Thanks
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
sorry it is attached now.
inputs.txt
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
suggestion:
I think if we can pick only all values i.e
value="2.0"
value="4314229999999913"
value="13"

and then looping it should do the work. is n't that guvera?
0
 
LVL 8

Expert Comment

by:guvera
Comment Utility
0
 
LVL 8

Expert Comment

by:guvera
Comment Utility
before try this one add namespace

using System.Text.RegularExpressions;


http://msdn.microsoft.com/en-us/library/30wbz966%28VS.71%29.aspx
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
String regexPattern = "value='[^*']";
                MatchCollection regexMatches = Regex.Matches(secondResponseFromServer, regexPattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);

 it matches only beginning single character of each value.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 8

Expert Comment

by:guvera
Comment Utility
Hi Dinesh,

Certainly, using Regular Expression is one approach, however, that would involve a lot of hard work creating the regex. I would recommend that you use the HTML Agility Pack, which you can download from here:

http://htmlagilitypack.codeplex.com/

It does a lot of the heavy lifting that you need to do in order to parse the HTML.

Hope that helps!
0
 
LVL 4

Expert Comment

by:daletian
Comment Utility
If u wish to match all your values, use
String regexPattern = "value='[a-zA-Z0-9.-:/]";
0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 234 total points
Comment Utility
Please check this as well:





String regexPattern = "value=\".*\"";

MatchCollection regexMatches = Regex.Matches(secondResponseFromServer, regexPattern, RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
This should give you just the value of each "value":
using System.Text.RegularExpressions;

Regex reg = new Regex("(?<=INPUT[^>]*?value=[\"']?)[^\"'>]*");
MatchCollection matches = reg.Matches(source_string);

foreach (Match m in matches)
{
    //m.Value holds the matched text
}

Open in new window

0
 

Author Comment

by:Dinesh Kumar
Comment Utility
Kaufmed, please explain your regex!
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
keen to accept multiple solutions.

kaufmed, please make these clear:
@"value='.*'"
why @ here?

good Shahan_Developer! can you make clear RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline ?
0
 

Author Comment

by:Dinesh Kumar
Comment Utility
kaufmed and Shahan_Developer, waiting for your post!
0
 
LVL 74

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 116 total points
Comment Utility
>> kaufmed , please make these clear :
>> @" value=' . *'" >
> why @ here?

I don't see where I used it, but the @ in front of a string literal turns the string into a raw string where backslashes are literal characters and not the escape character that they normally are.

Pattern explanation : ( ? < = I N P U T [ ^ > ] * ? v a l u e = [ \ " ' ] ? ) [ ^ \ " ' > ] *

(?<= ... )       Positive lookbehind; find this, but don't consider it part of the match
INPUT          literal
[^>]*?           zero or more ( * ) of any chacter NOT ( [^ ... ] ) a closing bracket ( > ). Non-greedy ( ? ), so stop searching at the first match
value=          literal
["']?              zero or one ( ? ) of either ( [ ... ] ) a single or double quote. Note, the backslash is an escape for the doble quote and not part of the pattern
[^"'>]*          zero or more ( * ) of any character NOT ( "[^ ... ] ) a single or double quote or a closing bracket. Again, the double quote is escaped
0
 
LVL 19

Assisted Solution

by:Shahan Ayyub
Shahan Ayyub earned 234 total points
Comment Utility
Hi!

Does it helps you to understand the meaning of the two keywords you pointed out:
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regexoptions%28VS.95%29.aspx

I just changed the pattern from your existing pattern of ID: 34109831:

This can also work:

     String regexPattern = "value=\".*\"";
     MatchCollection regexMatches = Regex.Matches(secondResponseFromServer, regexPattern);
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now