Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(formula): fix REGEXMATCH formula calculation errors (#6384)

wpxp123456964a522992ae
diff --git a/packages/engine-formula/src/engine/utils/regexp-check.ts b/packages/engine-formula/src/engine/utils/regexp-check.ts
index 6c03ce8291..08f4186369 100644
--- a/packages/engine-formula/src/engine/utils/regexp-check.ts
+++ b/packages/engine-formula/src/engine/utils/regexp-check.ts
@@ -694,23 +694,24 @@ function isSafeRegExp(regExp: RegExp, options?: IOptions): boolean {
let _depth = depth;
if (token.type === CharTypes.REPETITION) {
- _depth++;
- count++;
-
- if (_depth > 1) {
+ if (depth > 0 && (token.min === 0 || token.max === Infinity)) {
return false;
- }
+ };
- if (count > limit) {
- return false;
+ if (isDangerousRepetition(token)) {
+ count++;
+
+ if (count > limit) {
+ return false;
+ }
}
+
+ _depth = depth + 1;
}
if (token.options) {
for (const option of token.options) {
- const result = validateToken({ stack: option }, _depth);
-
- if (!result) {
+ if (!validateToken({ stack: option }, _depth)) {
return false;
}
}
@@ -723,9 +724,7 @@ function isSafeRegExp(regExp: RegExp, options?: IOptions): boolean {
}
for (const item of stack) {
- const result = validateToken(item, _depth);
-
- if (!result) {
+ if (!validateToken(item, _depth)) {
return false;
}
}
@@ -735,3 +734,24 @@ function isSafeRegExp(regExp: RegExp, options?: IOptions): boolean {
return validateToken(tokens, 0);
}
+
+function isDangerousRepetition(token: IToken): boolean {
+ const stack = token.stack || (token.value && (token.value as IToken).stack);
+ if (!stack) return false;
+ return containsBacktracking(stack);
+}
+
+function containsBacktracking(stack: IToken[]): boolean {
+ for (const t of stack) {
+ if (t.options && t.options.length > 0) {
+ return true;
+ }
+
+ const subStack = t.stack || (t.value && (t.value as IToken).stack);
+
+ if (subStack && containsBacktracking(subStack)) {
+ return true;
+ }
+ }
+ return false;
+}
diff --git a/packages/engine-formula/src/functions/text/regexextract/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/regexextract/__test__/index.spec.ts
index 206dcd1fe2..ebdae2a2ec 100644
--- a/packages/engine-formula/src/functions/text/regexextract/__test__/index.spec.ts
+++ b/packages/engine-formula/src/functions/text/regexextract/__test__/index.spec.ts
@@ -128,12 +128,12 @@ describe('Test regexextract function', () => {
expect(getObjectValue(result)).toStrictEqual('0104');
text = StringValueObject.create('The total is 123.45 or € 987.65');
- regularExpression = StringValueObject.create('[€]\d{1,3}(,\d{3})*(.\d{2})?');
+ regularExpression = StringValueObject.create('[€]\\d{1,3}(,\\d{3})*(.\\d{2})?');
result = testFunction.calculate(text, regularExpression);
- expect(getObjectValue(result)).toStrictEqual(ErrorType.REF);
+ expect(getObjectValue(result)).toStrictEqual(ErrorType.NA);
text = StringValueObject.create('Visit our website at https://www.example.com');
- regularExpression = StringValueObject.create('https?://[^\s]+');
+ regularExpression = StringValueObject.create('https?://[^\\s]+');
result = testFunction.calculate(text, regularExpression);
expect(getObjectValue(result)).toStrictEqual('https://www.example.com');
});
diff --git a/packages/engine-formula/src/functions/text/regexmatch/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/regexmatch/__test__/index.spec.ts
index e91d869902..db1c776ae1 100644
--- a/packages/engine-formula/src/functions/text/regexmatch/__test__/index.spec.ts
+++ b/packages/engine-formula/src/functions/text/regexmatch/__test__/index.spec.ts
@@ -90,9 +90,18 @@ describe('Test regexmatch function', () => {
});
it('More test', () => {
- const text = StringValueObject.create('13912345678');
- const regularExpression = StringValueObject.create('^1[3456789]\\d{9}$');
- const result = testFunction.calculate(text, regularExpression);
+ let text = StringValueObject.create('13912345678');
+ let regularExpression = StringValueObject.create('^1[3456789]\\d{9}$');
+ let result = testFunction.calculate(text, regularExpression);
+ expect(getObjectValue(result)).toStrictEqual(true);
+
+ text = StringValueObject.create('abcdefghi');
+ regularExpression = StringValueObject.create('(\\w{1,2}){2}');
+ result = testFunction.calculate(text, regularExpression);
+ expect(getObjectValue(result)).toStrictEqual(true);
+
+ regularExpression = StringValueObject.create('(\\w{1,2})(\\w{1,2})');
+ result = testFunction.calculate(text, regularExpression); ;
expect(getObjectValue(result)).toStrictEqual(true);
});
});