Commit 879ed119 authored by nanahira's avatar nanahira

use recursiveMatch

parent 4990bdd8
......@@ -15,6 +15,7 @@ import moment from 'moment';
import { chineseCharacterList } from './chinese-replace';
import { Worker } from 'tesseract.js';
import _ from 'lodash';
import { recursiveMatch } from './utils';
export * from './config';
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 =
/(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
this.log.info(`Parsing message from ${sender}: ${receivedMessage}`);
let messageMatch = receivedMessage.match(matcherGlobal);
let messageMatch = recursiveMatch(receivedMessage, matcherSingle);
if (useCache) {
const lastMessage = await this.cache.get('lastMessages', sender);
const currentMessage = receivedMessage;
......@@ -159,7 +158,7 @@ export default class HisoutensokuJammerPlugin
receivedMessage = `${lastMessage} ${receivedMessage}`;
this.log.info(`Merged message from ${sender}: ${receivedMessage}`);
if (!messageMatch) {
messageMatch = receivedMessage.match(matcherGlobal);
messageMatch = recursiveMatch(receivedMessage, matcherSingle);
}
}
if (!messageMatch) {
......@@ -171,8 +170,7 @@ export default class HisoutensokuJammerPlugin
return;
}
const results: { address: string; port: number }[] = [];
for (const pattern of messageMatch) {
const patternMatch = pattern.match(matcherSingle);
for (const patternMatch of messageMatch) {
if ((patternMatch?.length || 0) <= 6) {
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