Skip to content

[PORT][Expression] Support escape in regex #1938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion libraries/adaptive-expressions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"@types/node": "^10.12.18",
"nyc": "^15.0.0",
"ts-node": "^4.1.0",
"typescript": "3.5.3"
"typescript": "3.5.3",
"mocha": "^5.2.0"
},
"scripts": {
"build": "tsc",
Expand Down
9 changes: 9 additions & 0 deletions libraries/adaptive-expressions/src/CommonRegex.g4
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ option_flag
| 's'
;

// QUOTING
//
// \x where x is non-alphanumeric is a literal x
// \Q...\E treat enclosed characters as literal
Quoted : '\\' NonAlphaNumeric;
BlockQuoted : '\\Q' .*? '\\E';

atom
: shared_atom
| literal
Expand Down Expand Up @@ -152,6 +159,8 @@ shared_literal
| CarriageReturn
| Tab
| HexChar
| Quoted
| BlockQuoted
| OpenBrace
| CloseBrace
| Comma
Expand Down
2 changes: 1 addition & 1 deletion libraries/adaptive-expressions/src/commonRegex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CommonRegex {
result = this.regexCache.get(pattern);
} else {
if (!pattern || !this.isCommonRegex(pattern)) {
throw new Error(`A regular expression parsing error occurred.`);
throw new Error(`'${ pattern }' is not a valid regex.`);
}

result = this.getRegExpFromString(pattern);
Expand Down
2 changes: 1 addition & 1 deletion libraries/adaptive-expressions/src/expressionFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3057,7 +3057,7 @@ export class ExpressionFunctions {
error = 'regular expression is empty.';
} else {
const regex: RegExp = CommonRegex.CreateRegex(args[1]);
value = regex.test(args[0]);
value = regex.test(args[0].toString());
}

return {value, error};
Expand Down
689 changes: 351 additions & 338 deletions libraries/adaptive-expressions/src/generated/CommonRegexLexer.ts

Large diffs are not rendered by default.

634 changes: 330 additions & 304 deletions libraries/adaptive-expressions/src/generated/CommonRegexParser.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions libraries/adaptive-expressions/tests/expressionParser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ const dataSource = [
['isMatch(\'12abc\', \'([0-9]+)([a-z]+)([0-9]+)\')', false], // "(...)" (simple group)
['isMatch("a", "\\\\w{1}")', true], // "\w" (match [a-zA-Z0-9_])
['isMatch("1", "\\\\d{1}")', true], // "\d" (match [0-9])
['isMatch("12.5", "[0-9]+(\\\\.5)")', true], // "\." (match .)
['isMatch("12x5", "[0-9]+(\\\\.5)")', false], // "\." (match .)

//Type Checking Tests
['isString(hello)', true],
Expand Down