Time tracking app arbtt case insensitive match in rules - arbtt

is it possible to use modifiers on regex? Like:
m/some.*title/i ==> tag...

Yes: arbtt uses perl-compatible regexes, and they allow you to set modifiers inside the pattern:
m/(?i)some.*title/ ==> tag...
See https://www.pcre.org/original/doc/html/pcrepattern.html#SEC13 for more details.

Related

Regular expressions: How to combine AND & OR operators

I'm currently trying to perform a single regular expression which combine AND / OR operators but can't find a way to deal with it.
I'm using the PHP PCRE regex engine.
I've a text and I want to check the matching regarding rules which are allowing / disallowing words.
Example :
Rule allow: I want all to allow text which contains some
Rule disallow: I want to disallow if the text contains some page
Text 1: I found some trick.
Text 2: I found some pages in the book.
For the moment I'm computing a unique regex to check if the text does not match :
//staring with not allowing
//continue with disallowing
/^(?=(?!(some.*)))(?=(some page)).*$/
This is my problem, if the allow rule contains the disallow rule, I can't get a valid regex. This never match.
I've checked with other regex operators but can't found a valid way to build my pattern.
I want to generate a unique regex here to push the lines of the regex capabilities (I know for the moment :)).
This process perfectly works when the allow does not contains the disallow :
Rule allow: I want to allow if the text contains some page
Rule disallow: I want to disallow is the text which contains some
In this order it works...
Could you just use a negative lookahead
some(?! text)
https://regex101.com/r/bA7eV2/3

How to make this regular expression Pattern case insensitive - for list of words [duplicate]

Given this regular expression: "^[0-9]*\s*(lbs|kg|kgs)$" how do I make it case insensitive? I am trying to use this in a .net regular expression validator, so I need to specify case insensitivity in the pattern.
I can not use the RegexOptions programatically because I am specifying the regular expression in a RegularExpressionValidator
I found out.
Case sensitive: ^[0-9]\s(lbs|kg|kgs)$
Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)
I believe that this is specific to the .NET implementation of regular expressions. So if you use this in the RegularExpressionValidator you have to turn off client side validation because the javascript regex parser will not recognize the ?i token.
Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.
This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.
CustomValidator:
<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />
Code behind:
protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
Regex re = new Regex(#"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
args.IsValid = re.IsMatch(args.Value);
}
Client-side validation function:
function cvWeight_Validate(sender, args) {
var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
args.IsValid = reWeight.test(args);
}
This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error * shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.
Can we make Regex case-insensitive in C#? : Yes
Set option inline via (?i) pattern construct or via RegexOptions.IgnoreCase param
Can we make Regex case-insensitive in JavaScript? : Yes
Set flag via /pattern/flags syntax or the insensitive flag /REGEX/i
(Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute? : No
As Chris R. Timmons points out on RegularExpressionValidator doesn't ignore case:
There is no property in the RegularExpressionValidator control that
allows regex options to be set.
If your control does both client-side and server-side validation, the
regex must use a subset of regular expression syntax that both
JS and .NET can execute. In this case, to make a regex ignore
case it is necessary to use a character class construct like [a-zA-Z]
to match both upper and lower case characters.
If your validation is done on the server-side only, you can use the
more powerful .NET regular expression syntax. In this case, you can
place the (?i) option at the beginning of the regex to tell it to
ignore case.
So, if you want to use the out of the box validator, you're stuck with Geoff's solution of using character sets like this: [aA]
Easiest here is to just modify the regex to
^[0-9]*\s*([lL][bB][sS]|[kK][gG][sS]?)$
It's awful to read, but it will work fine.

How do I create a Scala Regex that is compiled using Java Pattern.COMMENTS?

I want to create several rather complex regular expressions used by my Scala code that take advantage of the Pattern.COMMENTS flag? I want to do something vaguely like this:
val regex = """my
(complex|hideous) # either is appropriate
pattern
(might)? # optional
look like this
""".r
(With the .r at the end of the string giving me all of Scala's Regex goodness)
Unfortunately, using .r doesn't give me any way to tell the Regex to use java.util.regex.Pattern.COMMENTS. Is there an way to create a scala.util.matching.Regex that compiles its source string with comments turned on?
According to the documentation, you should be able to use inline modifiers:
val regex = """(?x)my
(complex|hideous) # either is appropriate
pattern
(might)? # optional
look like this
""".r
See also the Java doc for Regex comments.
With an inline modifier, you enable the option from the point on, where the inline modifier is written. If you use it at the start, it is valid for the whole regular expression.
Check also regular-expressions.info for a further explanation

Regular expression to extract part of a file path using the logstash grok filter

I am new to regular expressions but I think people here may give me valuable inputs. I am using the logstash grok filter in which I can supply only regular expressions.
I have a string like this
/app/webpf04/sns882A/snsdomain/logs/access.log
I want to use a regular expression to get the sns882A part from the string, which is the substring after the third "/", how can I do that?
I am restricted to regex as grok only accepts regex. Is it possible to use regex for this?
Yes you can use regular expression to get what you want via grok:
/[^/]+/[^/]+/(?<field1>[^/]+)/
for your regex:
/\w*\/\w*\/(\w*)\/
You can also test with:
http://www.regextester.com/
By googling regex tester, you can have different UI.
If you are indeed using Perl then you should use the File::Spec module like this
use strict;
use warnings;
use File::Spec;
my $path = '/app/webpf04/sns882A/snsdomain/logs/access.log';
my #path = File::Spec->splitdir($path);
print $path[3], "\n";
output
sns882A
This is how I would do it in Perl:
my ($name) = ($fullname =~ m{^(?:/.*?){2}/(.*?)/});
EDIT:
If your framework does not support Perl-ish non-grouping groups (?:xyz), this regex should work instead:
^/.*?/.*?/(.*?)/
If you are concerned about performance of .*?, this works as well:
^/[^/]+/[^/]+/([^/]+)/
One more note: All of regexes above will match string /app/webpf04/sns882A/.
But matching string is completely different from first matching group, which is sns882A in all three cases.
Same answer but a small bug fix. If you doesnt specify ^ in starting,it will go for the next match(try longer paths adding more / for input.). To fix it just add ^ in the starting like this. ^ means starting of the input line. finally group1 is your answer.
^/[^/]+/[^/]+/([^/]+)/
If you are using any URI paths use below.(it will handle path aswell as URI).
^.*?/[^/]+/[^/]+/([^/]+)/

Regular Expression to check for words

Here is my regular expression:
#"\<\\{+(TITLE|BODY|DATE|CALENDARID|CALENDARSUBSCRIPTIONGUID|CALENDARURL|TIME|CONTACTS|LOCATION|URLINFO)\\}+\\>"
It needs to check for words like {Title}. Have I made it correctly?
It looks ok to me. You might want to have the -i switch to make your expression case insensitive unless you only want to match "TITLE" and not "title" for example. Here is a pretty good regex checker: RegEx Pal. I noticed you didn't flag a language and the previous link was a javascript regex tester. Here are some more resources Regex Lib resources page