Commit 879ed119 authored by nanahira's avatar nanahira

use recursiveMatch

parent 4990bdd8
...@@ -15,6 +15,7 @@ import moment from 'moment'; ...@@ -15,6 +15,7 @@ import moment from 'moment';
import { chineseCharacterList } from './chinese-replace'; import { chineseCharacterList } from './chinese-replace';
import { Worker } from 'tesseract.js'; import { Worker } from 'tesseract.js';
import _ from 'lodash'; import _ from 'lodash';
import { recursiveMatch } from './utils';
export * from './config'; export * from './config';
declare module 'koishi' { declare module 'koishi' {
...@@ -26,8 +27,6 @@ declare module 'koishi' { ...@@ -26,8 +27,6 @@ declare module 'koishi' {
} }
} }
const matcherGlobal =
/(0*[^\d]*)*([1-2]?[^\d\.]?\d{0,2})(([^\d]+[1-2]?\d{1,2}){3,}?).+?([1-6]([^\d]*\d){4})(.*[\+\-]\d{1,5})*/g;
const matcherSingle = const matcherSingle =
/(0*[^\d]*)*([1-2]?[^\d\.]?\d{0,2})(([^\d]+[1-2]?\d{1,2}){3,}?).+?([1-6]([^\d]*\d){4})(.*[\+\-]\d{1,5})*/; /(0*[^\d]*)*([1-2]?[^\d\.]?\d{0,2})(([^\d]+[1-2]?\d{1,2}){3,}?).+?([1-6]([^\d]*\d){4})(.*[\+\-]\d{1,5})*/;
...@@ -151,7 +150,7 @@ export default class HisoutensokuJammerPlugin ...@@ -151,7 +150,7 @@ export default class HisoutensokuJammerPlugin
this.log.info(`Parsing message from ${sender}: ${receivedMessage}`); this.log.info(`Parsing message from ${sender}: ${receivedMessage}`);
let messageMatch = receivedMessage.match(matcherGlobal); let messageMatch = recursiveMatch(receivedMessage, matcherSingle);
if (useCache) { if (useCache) {
const lastMessage = await this.cache.get('lastMessages', sender); const lastMessage = await this.cache.get('lastMessages', sender);
const currentMessage = receivedMessage; const currentMessage = receivedMessage;
...@@ -159,7 +158,7 @@ export default class HisoutensokuJammerPlugin ...@@ -159,7 +158,7 @@ export default class HisoutensokuJammerPlugin
receivedMessage = `${lastMessage} ${receivedMessage}`; receivedMessage = `${lastMessage} ${receivedMessage}`;
this.log.info(`Merged message from ${sender}: ${receivedMessage}`); this.log.info(`Merged message from ${sender}: ${receivedMessage}`);
if (!messageMatch) { if (!messageMatch) {
messageMatch = receivedMessage.match(matcherGlobal); messageMatch = recursiveMatch(receivedMessage, matcherSingle);
} }
} }
if (!messageMatch) { if (!messageMatch) {
...@@ -171,8 +170,7 @@ export default class HisoutensokuJammerPlugin ...@@ -171,8 +170,7 @@ export default class HisoutensokuJammerPlugin
return; return;
} }
const results: { address: string; port: number }[] = []; const results: { address: string; port: number }[] = [];
for (const pattern of messageMatch) { for (const patternMatch of messageMatch) {
const patternMatch = pattern.match(matcherSingle);
if ((patternMatch?.length || 0) <= 6) { if ((patternMatch?.length || 0) <= 6) {
continue; continue;
} }
......
export function recursiveMatch(
content: string,
regex: string | RegExp,
): RegExpMatchArray[] | null {
const match = content.match(regex);
if (!match) return null;
const remainingContent = content.slice(match.index + match[0].length);
if (!remainingContent) return [match];
const remainingMatches = recursiveMatch(remainingContent, regex) || [];
return [match, ...remainingMatches];
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment