I want to keep default values in regular expressions if pattern does not match - python-textfsm

I want to keep default values in regular expressions if the pattern does not match
Value LINK_GROUP_NAME (\S+)
Start
^ link-group ${LINK_GROUP_NAME}
Above is my python textfsm template. Here if my regular expression doesn't match then I want to keep the default value instead of discarding.

Related

How to use variable character strings with 'substitute' function in R

I need to have the possibility to fill an expression with the values of the unknown number of variables. The shape of the expression depends on the number of the variables.
Example:
Expression1: "italic(y)==a*italic(x)*b"
to become: "y=1.2 x+4.3"
Expression2: "italic(y)==a*italic(x)*b~c"
to become: "y=1.2 x+4.3 -5.3"
Currently I am using the substitute function, but it does not work along with the expression function:
substitute(expression("italic(y)==a*italic(x)*b"),list(a=1.23,b=2.3))
My expression needs to grow as the number of variables (i.e. length of the list) increases. So, next step would be to add the variable c:
substitute(expression("space1*italic(y)==a*italic(x)*b*c"),list(a=1.23,b=2.3,c=3.2))
But I need to change the expression in the code without any manual interference and these codes do not read the variable values from the list unless I change it to this (in which the expression is not expandable anymore as it is not a string):
substitute(italic(y)==a*italic(x)*b*c,list(a=1.23,b=2.3,c=3.2))
How can I do this?
Here is a script which might be along the lines of what you want. We can iterate the list of replacements using a for loop, and then make a regex replacement of the placeholder in the expression with the corresponding value from the list.
lst <- list(a=1.23,b=2.3)
expression <- "italic(y)==a*italic(x)*b"
for (name in names(lst)) {
expression <- gsub(paste0("\\b", name, "\\b"), lst[[name]], expression)
}
print(expression)
[1] "italic(y)==1.23*italic(x)*2.3"
Note carefully that I search for the variable name surrounded by word boundaries on both sides. If your placeholder would ever be surrounded by other word characters, then my solution would fail, and we would need to change the replacement logic.

What is the zsh equivalent for $BASH_REMATCH[]?

What is the equivalent in zsh for $BASH_REMATCH, and how is it used?
Alternatively, one could simply use
$match[1]
in place of
$BASH_REMATCH[1]
To make zsh behave the same as bash, use:
setopt BASH_REMATCH
Or within a function consider:
setopt local_options BASH_REMATCH
(this will only set the option within the scope of the function)
Then just use $BASH_REMATCH as you would in bash.
The manual says about BASH_REMATCH:
When set, matches performed with the =~ operator will set the BASH_REMATCH array variable, instead of the default MATCH and match variables. The first element of the BASH_REMATCH array will contain the entire matched text and subsequent elements will contain extracted substrings. This option makes more sense when KSH_ARRAYS is also set, so that the entire matched portion is stored at index 0 and the first substring is at index 1. Without this option, the MATCH variable contains the entire matched text and the match array variable contains substrings.
Then =~ will behave like in bash, but if you want the full behaviour as described in the manual:
string =~ regexp
true if string matches the regular expression regexp. If the option RE_MATCH_PCRE is set regexp is tested as a PCRE regular expression using the zsh/pcre module, else it is tested as a POSIX extended regular expression using the zsh/regex module. Upon successful match, some variables will be updated; no variables are changed if the matching fails.
If the option BASH_REMATCH is not set the scalar parameter MATCH is set to the substring that matched the pattern and the integer parameters MBEGIN and MEND to the index of the start and end, respectively, of the match in string, such that if string is contained in variable var the expression ‘${var[$MBEGIN,$MEND]}’ is identical to ‘$MATCH’. The setting of the option KSH_ARRAYS is respected. Likewise, the array match is set to the substrings that matched parenthesised subexpressions and the arrays mbegin and mend to the indices of the start and end positions, respectively, of the substrings within string. The arrays are not set if there were no parenthesised subexpresssions. For example, if the string ‘a short string’ is matched against the regular expression ‘s(...)t’, then (assuming the option KSH_ARRAYS is not set) MATCH, MBEGIN and MEND are ‘short’, 3 and 7, respectively, while match, mbegin and mend are single entry arrays containing the strings ‘hor’, ‘4’ and ‘6’, respectively.
If the option BASH_REMATCH is set the array BASH_REMATCH is set to the substring that matched the pattern followed by the substrings that matched parenthesised subexpressions within the pattern.

Regular expression for excluding some specific characters

I am trying to build a regular expression in Qt for the following set of strings:
The set can contain all the set of strings of length 1 which does not include r and z.
The set also includes the set of strings of length greater than 1, which start with z, followed by any number of z's but must terminate with a single character that is not r and z
So far I have developed the following:
[a-qs-y]?|z+[a-qs-y]
But it does not work.
The question mark in your regular expression causes the first alternative to either match lowercase strings of length 1 excluding r and z or the empty string, and as the empty string can be matched within any string, the second alternative will never be matched against. The rest of your regular expression matches your specification, although you will probably want to make your regular expression only match entire strings by anchoring it:
QRegularExpression re("^[a-qs-y]$|^z+[a-qs-y]$");
QRegularExpressionMatch match = re.match("zzza");
if (match.hasMatch()) {
QString matched = match.captured(0);
// ...
}

ASPX attribute regex parsing in c#

I need to find attribute values in an ASPX file using regular expressions.
That means you don't need to worry about malformed HTML or any HTML related issues.
I need to find the value of a particular attribute (LocText). I want to get what's inside the quotes.
Any ASPX tags such as <%=, <%#, <%$ etc. inside the value don't make sense for this attribute therefore are considered as part of it.
The regex I began with looks like this:
LocText="([^"]+)"
This works great, the first group, which is the result text, gets everything except the double quotes, which are not allowed there (&quot ; must be used instead)
But the ASPX file allows using of single quotes - second regular expression must be applied then.
LocText='([^']+)'
I could use these two regular expressions but I'm looking for a way to connect them.
LocText=("([^"]+)"|'([^']+)')
This also works but doesn't seem very efficient as it's creating unnecessary number of groups. I think this could be somehow done by using backreferences, but I can't get it to work.
LocText=(["']{1})([^\1]+)\1
I thought that by this, I save the single/double quote to the first group and then I tell it to read anything that is NOT the char found in the first group. This is enclosed again by the quote from the first group. Obviously, I'm wrong and it's not working like that.
Is there any way, how to connect the first two expressions together creating just a minimum amount of groups with one group being the value of the attribute I want to get? Is it possible using a backreference for the single/double quote value, or have I completely misunderstood the meaning of them?
I'd say your solution with alternation isn't that bad, but you could use named captures so the result will always be found in the same group's value:
Regex regexObj = new Regex(#"LocText=(?:""(?<attr>[^""]+)""|'(?<attr>[^']+)')");
resultString = regexObj.Match(subjectString).Groups["attr"].Value;
Explanation:
LocText= # Match LocText=
(?: # Either match
"(?<attr>[^"]+)" # "...", capture in named group <attr>
| # or match
'(?<attr>[^']+)' # '...', also capture in named group <attr>
) # End of alternation
Another option would be to use lookahead assertions ([^\1] isn't working because you can't place backreferences inside a character class, but you can use them in lookarounds):
Regex regexObj = new Regex(#"LocText=([""'])((?:(?!\1).)*)\1");
resultString = regexObj.Match(subjectString).Groups[2].Value;
Explanation:
LocText= # Match LocText=
(["']) # Match and capture (group 1) " or '
( # Match and capture (group 2)...
(?: # Try to match...
(?!\1) # (unless it's the quote character we matched before)
. # any character
)* # repeat any number of times
) # End of capturing group 2
\1 # Match the previous quote character

regular validation expression in asp.net

i have got a code to edit some function. There is a text box in that web application. It using a regular expression validator control to validate the text box. the validation expression is
ValidationExpression="[\w]{3,15}"
it accept all letters,numbers and underscores. but it do not accept special characters like \,/ * . i want to change the above regular expression to accept / .
i hope someone can explain what the above regular expression means and how to change that expression to accept / without affecting current regular expression
i am using asp.net and c#
string ValidationExpression= "[\w/]{3,15}"
[...] match a single character presents in the list between brackets
[...]{3,15} match between 3 and 15 characters presents between brackets
\w match a word character (letter, digit, underscore...)
/ match the character /
So [\w/]{3,15} match a word character or '/' between 3 and 15 times.
You current regular expression can be deconstructed as follows :
[] brackets represents regular expression group. Regex engine will try to match all the characters or group of characters given inside [] with the input string.
\w - Allow all the alpha numberic characters which includes upper case and lower case alphabets and 0 to 9 numbers and and underscore (This does not include other special characters like / or # ,etc ).
{3,15} means minimum 3 and maximum 15 alphanumeric characters must be provided in order to successfully match the string.
To add other charters, you need to add them explicitly. If you want to add / your regex should be like [\w/]{3,15}.
You can learn everything about regex here.

Resources