Find text between key phrases - regex

I have a var that have some text in:
<cfsavecontent variable="foo">
element.password_input=
<div class="holder">
<label for="$${input_id}" > $${label_text}</label>
<input name="$${input_name}" id="$${input_id}" value="$${input_value}" type="password" />
</div>
# END element.password_input
element.text_input=
<div class="ctrlHolder">
<label for="$${element_id}" > $${element_label_text}</label>
<input name="$${element_name}" id="$${element_id}"
value="$${element_value}" type="text"
class="textInput" />
</div>
# END element.text_input
</cfsavecontent>
and I am trying to parse through the var to get all of the different element type(s) here is what I have so far:
ar = REMatch( "element\.+(.*=)(.*?)*", foo )
but it is only giving me this part:
element.text_input=
element.password_input=
any help will be appreciated.

Your immediate problem is that by default . doesn't include newlines - you would need to use the flag (?s) in your regex for it to do this.
However, simply enabling that flag still wont result in your present regex doing what you're expecting it to do.
A better regex would be:
(element\.\w+)=(?:[^##]+|##(?! END \1))+(?=## END \1)
You would then do ListFirst(match[i],'=') and ListRest(match[i],'=') to get the name and value. (rematch doesn't return captured groups).
(Obviously the #s above are doubled to escape them for CF.)
The above regex dissected is:
(element\.\w+)=
Match element. and any alphanumeric, placed it into capture group 1, then match = character.
(?:
[^##]+
|
##(?! END \1)
)+
Match any number of non-hash characters, or a hash not followed by the ending token (using negative lookahead (?!...)) and referencing capture group 1 (\1), repeat as many times as possible (+), using a non-capturing group ((?:...)).
(?=## END \1)
Lookahead (?=...) to confirm the variable's ending token is present.

Related

Regex match multiple items

I have the following string:
<button {{ $attributes->class([
'bg-blue-600 hover:bg-blue-700 text-white px-3 py-2 rounded',
'bg-blue-600 px-3 py-2 hover:bg-blue-700 text-white rounded',
])->merge([
'wire:click' => $click,
]) }}>
{{ $label }}
</button>
I'm trying to get a VS Code extension (headwind) to match the stuff inside the class method single quotes via custom regex setting.
I have this regex which works a bit:
class\(([^)]*)\)
However the problem is its matching everything inside of the braces, which makes headwind mess up.
I need it to match each occurence of stuff inside the single quotes. How do I do this?
You can use
(?<=\\bclass\\(\\[\\s*'(?:[^']*'\\s*,\\s*')*)[^']+(?=')
I.e., the (?<=\bclass\(\[\s*'(?:[^']*'\s*,\s*')*)[^']+(?=') escaped version.
See the regex demo. Details:
(?<=\bclass\(\[\s*'(?:[^']*'\s*,\s*')*) - a position that is immediately preceded with a whole word class([, zero or more whitespaces, ', and then zero or more occurrences of any zero or more chars other than ', ', a , enclosed with zero or more whitespaces and then a ' char
[^']+ - one or more chars other than '
(?=') - a location that is immediately followed with a ' char.

Regex match 3 characters followed by integers

I am new to regex expression and I need a regex in the following pattern:
The string must have a format of “TCK#”. TCK followed by integers.
For example, This is acceptable TCK123. This is not acceptable 123
Here is my current regex expression:
input class="form-control" required="true" type="text" name="TCKInput"
pattern="^[TCK][0-9]$">
With my current code, when the user enter TCK123, it is not acceptable, which is not what I am looking for
Change to below regex:
^(?:TCK)[0-9]+$
Demo: https://regex101.com/r/h9V7n1/1
Changes in the existing Regex you were using:
1) You were using [, ] around TCK which means regex has to match
any one of the values inside this bracket. As you have to match TCK
as it is, change it to (, )
2) You didn't mention + after [0-9] which means exactly one
occurrence will be matched. However, if you will mention +, it will
match one or more occurrence
If you want all 3 letters: TCK and then at least one or more digits after it, then try this:
^TCK\d+$
If you use [TCK] that will only accept one T, one C, or one K
Demo
This Demo sends to a live test server, so a successful submission of data will result in a response from said server
<form id='main' action='https://httpbin.org/post' method='post'>
<input class="form-control" required="true" type="text" name="TCKInput" pattern="^TCK\d+$">
<input type='submit'>
</form>

Regex not working in HTML5 pattern

So I have this regex intended to let pass all text but those that contain as initial chars the "34" sequence:
^(?!34)(?=([\w]+))
The regex is working fine for me in https://regex101.com/r/iN1yN3/2 , check the tests to see the intended behavior.
Any Idea why it isn't working in my form?
<form>
<input pattern="^(?!34)(?=([\w]+))" type="text">
<button type="submit">Submit!</button>
</form>
The pattern attribute has to match the entire string. Assertions check for a match, but do not count towards the total match length. Changing the second assertion to \w+ will make the pattern match the entire string.
You can also skip the implied ^, leaving you with just:
<input pattern="(?!34)\w+" type="text">

Why does Regex Replace delete a quote?

I'm trying to sanitize HTML tags, e.g. turn
<input type="image" name="name" src="image.png">
into the correct empty-element form
<input type="image" name="name" src="image.png" />
with a slash at the end.
I'm using Eclipse's Find/Replace with regular expressions like this:
Find: <(input .*)[^/]>
Replace with: <\1 />
But I end up with
<input type="image" name="name" src="image.png />
I.e. the last quote is missing.
Is that an error in my regex, or a bug in Eclipse?
The term [^/] is consuming the quote. Move it inside the captured group:
Find: <(input .*[^/])>
Replace: <\1 />
The error is in your regex. The [^/] at the end captures the last non-> character. \1 represents the first capturing group, which would be (input.*). In short, you are getting everything inside the tag except the last character. If you put the [^\] inside your group, your replace should work.
Also, you may run into issues if you have a / inside of one of your attribute values. For performance reasons, I would recommend using the following regex:
<(input [^/]*(/[^/]*)*)>
In this case, it does not have to backtrack if you have a / inside of one of your attributes. Your regex should capture everything you need though.

Angular ng-pattern does not check upper character limit of my regular expressions

I am just starting to test out ng-pattern with some regular expressions that I have used in non-Angular projects and have worked fine. But using ng-pattern they don't seem to work. For example I have this regular expression that successfully checks for a string of 6-20 characters with at least 1 alphabetical and 1 numeric character :
"^.*(?=.{6,20})(?=.*\d)(?=.*[a-zA-Z]).*$"
However, in my Angular example below it checks everything successfully except it is not triggered when the string goes beyond 20 characters :
<div class="controls">
<input type="text" ng-model="user.Password" id="Password" name="Password" title="Password" required ng-pattern="^/.*(?=.{6,20})(?=.*\d)(?=.*[a-zA-Z]).*$/" />
<span ng-show="form.Password.$dirty && form.Password.$error.required">{{'_PasswordRequired_' | i18n}}</span>
<span ng-show="form.Password.$dirty && form.Password.$error.pattern">{{'_PasswordLengthAndAlphanumeric_' | i18n}}</span>
</div>
Is there some error I am making in the syntax, or is there some other reason this is not working?
You're missing an end anchor, and you have one .* too many (at the start):
^ # Start of string
.* # Match *any* number of characters
(?=.{6,20}) # *Then* check that 6-20 characters follow
<snip>
.* # *Then* match any number of characters anyway
$ # until the end of the string
This would work:
"^(?=.{6,20}$)(?=.*\d)(?=.*[a-zA-Z]).*$"
But it would be easier (and more obvious) to do the length check outside of the lookahead anyway:
"^(?=.*\d)(?=.*[a-zA-Z]).{6,20}$"
(which in turn makes me ask why you're imposing such a low upper limit? My KeePass-generated passwords are usually at least 30 characters in length, for example)