How do I deal with this regex issue? - regex

I need to extract a value from a hidden HTML field, have somewhat figured it out but I'm currently stuck.
My regex looks like:
<input type="hidden" name="form_id" value=".*"
But this extracts the whole string from the HTML.
The string looks like:
<input type="hidden" name="form_id" value="123"/>
I need to extract the "value" from the string, it is always changing, but the "name" is always the same. Is there a way to extract it without doing another expression? I appreciate any help.

(?<=<[^<>]+?name="form_id"[^<>]+value=")(.*)(?=")

I just threw this together. Basically you want to negate any ending > in your request. So you'd likely want to do something of this nature:
<[^>]*hidden[^>]*value="(.*)"[^>]*>
And then read the first capture group (Delphi instructions). This keeps it as reasonably generic as possible although it does assume positional order on "hidden" and "value".
In order to find the value without regard for order you could use could use a slightly cleaner lookahead as was suggested:
^(?=.*name="form_id").*value="([^"]*)".*$

<[a-zA-Z"= _^>]*value="(\d*)"/>
I have tested this for your example.
If you want to extract for only input tag you can write:
<input[a-zA-Z"= _^>]*value="(\d*)"/>

Related

How to extract html attributes via regex

I am looking to see how a regex can be used to get attribute/values from an html tag. Yes I know that an xml/html parser can be used, but this is for testing my ability in regex. For example, in this html element:
<input name=dir value=">">
<input value=">" name=dir >
How would I extract out:
(?<name>...) and (?<value>...)
Is it possible once you have matched something to go "back" to the start of the match? For example:
<(?P<element>\w+).+(?:value="(?P<value>[^"])")####.+(?:name="(?P<name>[^"])")
Where #### basically means "go back to the start of the previous match/capture group (so that I don't have to modify every possible ordering of the tags). How could this be done?
Yes, using a parser is the best way.
As stated in the comments, you cannot (easily) extract all information in one sweep.
You can achieve what you want with several regexes:
input.*?name=(?'name'[^ ]+)
Test here.
input.*?value="(?'value'[^"]+)"
Test here.

HTML input pattern fails

I'm sure I miss something, but can't find the reason why this pattern doesn't work... The validator doesn't accept the format of the string I typed (i.e. 06201234567).
<input type="tel" pattern="06\d{7,9}" placeholder="06201234567">
I tried exactly the same code at w3schools' tryit editor, and there were no problem...
In HTML5 you can use <input type='tel'> and <input type='email'>
You can also specify a specific pattern like <input type='tel' pattern='[\+]\d{2}[\(]\d{2}[\)]\d{4}[\-]\d{4}' title='Phone Number (Format: +99(99)9999-9999)'>
Something like pattern='^\+?\d{0,13}' Would give you an optional + and up to 13 digits
The form is in a template where I replaced some texts like {{example}} with other texts and php's preg_replace() search expression {{.*?}} matched {7,9} in the pattern and replaced it.
With the use of ({{.*?}}) everything's OK.

Validate int value with regular expression

I want to validate that input only allow maximum value: 2,147,483,647. But it's not validate at all. What is wrong in there regular expression.
<input
pattern="^(([1-9][0-9]{1,9})|([2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8]{10}))$"
required type="number" class="form-control" />
I think a regex may be a poor solution choice for what you are seeking. That said, below is what I believe to be a correct regex for what you are seeking to do:
^(0*(
[1-9][0-9]{1,9})|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6]4[0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3]6[0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8]3[0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4]8[0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7]4[0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4]7[0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1]4[0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2]1[0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
(2[0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-4][0-8])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-6][0-3][0-9])|
([1-2][0-1][0-4][0-7][0-4][0-8][0-3][0-5][0-9]{2})|
([1-2][0-1][0-4][0-7][0-4][0-8][0-2][0-9]{3})|
([1-2][0-1][0-4][0-7][0-4][0-7][0-9]{4})|
([1-2][0-1][0-4][0-7][0-3][0-9]{5})|
([1-2][0-1][0-4][0-6][0-9]{6})|
([1-2][0-1][0-3][0-9]{7})|
([1-2]0[0-9]{8})|
(1[0-9]{9})
)$
The whitespace and newlines are only for clarity and are not part of the regex. I do not guarantee its correctness though I think it is. You can try this out at a place like Regex 101.
As you can see this is complex and should probably be generated by a program instead of a human.
The ugliness of the answer hints that it is a poor choice of solution.

ng-pattern for not allowing input of a certain set of characters (e.g. +-!"¤%&/=?)

My question is exactly like my title. I'm writing a directive with an input field inside the template like below:
<input name="{{name}}"
id="{{name}}"
ng-model="message"
ng-maxlength="{{length}}"
maxlength="{{length}}"
type="text"
ng-pattern=" ... "
autocomplete="off"
/>
what should I fill in ng-pattern?
I finally use /[^0-9a-zA-ZøæåÆØÅöÖäÄüÜ\.\*\\,/\():\[\]= &-]/g
Thanks iismathwizard for mention the whitelist thing.
You can try this:
^[^+\-!"¤%&\/=?]+$
I'm not sure if you have more characters you need to disallow. Perhaps it would be easier to whitelist rather than blacklist "illegal" characters.
Regex101

Regex in angular- first digit not zero, but allow single zero

I want to have input element which allows one of two conditions:
Single zero can be entered
Number with max of 9 digits can be entered, but first digit shouldn't be zero
I wrote this regex (solution works in online regex testers):
/(^0$)|(^[1-9]\d{0,8}$)/
But when I use it in ng-pattern in Angular, it doesn't work.
Here is my plunker example:
http://plnkr.co/edit/iDQ7ly8ypJ3UmN5A0hJw?p=preview
Not sure if alternation is doing the problems, or I messed up the regex.
UPDATE: it seems that type="number" is causing problems. Unfortunately, I need to have this in my code, so I'm searching for solution which works with type="number".
This should work for you. I did the following:
Took out the type="number".
Gave the form a name.
Gave the input a name.
Referenced the form and input via their names instead of their id and ng-model values, respectively.
It converts the value to a number under the covers, stripping the leading zeros and converting text to 0, etc.. And the name is the correct way to access it as far as I can tell.
<form name="myForm">
<input name="myNumberField" ng-model="myNumber" ng-pattern="/(^0$)|(^[1-9]\d{0,8}$)/" required/>
<span ng-show="myForm.myNumberField.$error.pattern">Invalid pattern</span>
</form>
Here is a plunker for it.