none
Regex retrieve value from form post (I said the R word, sorry) RRS feed

  • Question

  • I have some data that is being read and populated into a DB. I have just on more thing to read from Regex, like always, when its the last thing and everything is going good, ha-Nope!.  This is hard coded on pages on my computer, not http.  I'm using visual studio 2017

    Here it is the form part:

     

    <input name="amount" type="hidden" value="1.99" />  so, I'm looking to extract 1.99 with no quotes.

    I have tried multiple variations: 

    (@"<input.*amount.*type=.hidden.*value=.*[(\d{1,3})(\,\d{3})*|(\d+)(\.\d{2})]."); works but tag included

    (@"<input\s.amount.\stype=.hidden.\svalue=.[(\d{1,3})(\,\d{3})*|(\d+)(\.\d{2})]."); no results returned.  

    I know I'm close, but working to hard makes you a dull boy. 

    Thanks in advance

    Monday, July 23, 2018 5:21 PM

All replies

  • If you only need the decimal number, you could try:
    string line = "<input name=\"amount\" type=\"hidden\" value=\"1.99\" />";
    string pattern = @"\d+\.\d+";
    string value = Regex.Match(line, pattern).Value;
    
    
    wizend
    Monday, July 23, 2018 5:41 PM
  • I have to target the value amount, there is multiple values in the page and in the form.  The string is is about 3000 character long that is read.  The escape sequence 

    string line = "<input name=\"amount\" type=\"hidden\" value=\"1.99\" />"; \""\ does not work in the IDE

    Monday, July 23, 2018 8:41 PM
  • Pull only one instance of 1.99, there is multiple values or prices. Sorry, should have stated explicitly there is multiple values for amounts
    Monday, July 23, 2018 8:57 PM
  • There is a series of third-party libraries that are able to parse HTML, allowing you to work with tree of objects instead of text. Can be installed using “Manage NuGet Packages” command.

    If you prefer Regular Expressions, try this too:

    <input\b(?=[^>]*\bname="amount")(?=[^>]*\btype="hidden")[^>]*\bvalue="(.*?)"[^>]*>


    Tuesday, July 24, 2018 6:16 AM
  • Hi Odinart,

    Thank you for posting here.

    For your question, if you only want to get 1.99 of value attribute, you could try the code below.

      string input = "<input name=\"amount\" type=\"hidden\" value=\"1.99\" /> ";
                Regex regex = new Regex(@"(?<=\bvalue="")[^""]*");
                Match match = regex.Match(input);
                string value = match.Value;
                Console.WriteLine(value);

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 24, 2018 7:26 AM
    Moderator
  • The problem I am having is with the qoutes: 

    Like Regex priEx = new Regex(@"<input\b(?=[^>]*\bname="amount")(?=[^>]*\btype=\"hidden\")[^>]*\bvalue=\"(.*?)\"[^>]^>"); 

    How do you escape the qoutes?   The double qoutes are not well liked in the ide. 

    I have tried Wu original post of <input\b(?=[^>]*\bname="amount")(?=[^>]*\btype="hidden")[^>]*\bvalue="(.*?)"[^>]*> and had zero results. 

    Thanks in advance

    Saturday, August 4, 2018 7:37 PM