test java String.matches again

本文探讨了Java中字符串匹配正则表达式的原理及常见陷阱,特别关注使用正则表达式进行匹配时可能导致的部分字符串无法正确匹配的问题,并提供了解决方案。

i found the String.matches must match the whole string,could not match part of string

http://stackoverflow.com/questions/4771447/java-string-matches-regex-with-or-pipe

 

this is a trap

 

 

package ZZZZ;

import java.util.regex.PatternSyntaxException;

public class cc {


    public static void main(String[] args) {
       
        String subjectString="我__";
        // TODO Auto-generated method stub
        try {
            String regex="[^a-zA-Z0-9]*$";
           
            boolean foundMatch = subjectString.matches(regex);
            System.out.println(foundMatch);
        } catch (PatternSyntaxException ex) {
            // Syntax error in the regular expression
            System.out.println(ex.toString());
        }

    }

}

// 字符串比较函数(用于排序) #define PATH_MAX 4096 // 比较函数用于排序 static int compare_strings(const void *a, const void *b) { return strcasecmp(*(const char **)a, *(const char **)b); } // 补全状态结构 typedef struct { char **matches; // 匹配路径数组 int match_count; // 匹配数量 int current_index; // 当前索引 char *base_text; // 初始化时的文本 } CompletionState; static CompletionState comp_state = {0}; // 重置状态 void reset_completion_state() { printf("Resetting completion state\n"); if (comp_state.matches) { for (int i = 0; i < comp_state.match_count; i++) { if (comp_state.matches[i]) { XFREE(MTYPE_TMP, comp_state.matches[i]); } } XFREE(MTYPE_TMP, comp_state.matches); comp_state.matches = NULL; } if (comp_state.base_text) { XFREE(MTYPE_TMP, comp_state.base_text); comp_state.base_text = NULL; } comp_state.match_count = 0; comp_state.current_index = 0; } // 文件补全函数 char *filename_completion_function(const char *text, int state) { printf("\nfilename_completion_function: state=%d text=%s\n", state, text); // 状态0:初始化或文本变化时重新初始化 if (state == 0 || (comp_state.base_text && strcmp(text, comp_state.base_text) != 0)) { printf("Initializing new completion for: %s\n", text); reset_completion_state(); // 保存当前文本作为基准 comp_state.base_text = XSTRDUP(MTYPE_TMP, text); printf("Set base_text: %s\n", comp_state.base_text); // 解析基础路径和前缀 const char *last_slash = strrchr(text, '/'); char base_path[PATH_MAX] = {0}; const char *prefix = text; if (last_slash) { // 提取目录部分(包括结尾斜杠) int base_len = last_slash - text + 1; if (base_len >= PATH_MAX) base_len = PATH_MAX - 1; strncpy(base_path, text, base_len); base_path[base_len] = '\0'; prefix = last_slash + 1; printf("Parsed: base_path='%s', prefix='%s'\n", base_path, prefix); } else { // 没有斜杠时,base_path 应为空字符串 base_path[0] = '\0'; prefix = text; printf("No slash: base_path='', prefix='%s'\n", prefix); } // 确定要打开的目录路径 const char *dir_to_open = (base_path[0] == '\0') ? "." : base_path; printf("Opening directory: %s\n", dir_to_open); // 打开目录 DIR *dir = opendir(dir_to_open); if (!dir) { printf("Cannot open directory: %s (error: %s)\n", dir_to_open, strerror(errno)); return NULL; } // 临时存储匹配项 char *temp_matches[256] = {0}; int count = 0; size_t prefix_len = strlen(prefix); struct dirent *entry; printf("Scanning directory entries...\n"); while ((entry = readdir(dir)) != NULL && count < 255) { // 跳过特殊目录 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } // 前缀匹配检查 if (prefix_len > 0 && strncasecmp(entry->d_name, prefix, prefix_len) != 0) { continue; } // 判断是否为目录 int is_dir = (entry->d_type == DT_DIR); // 构建完整路径 int path_len = strlen(base_path) + strlen(entry->d_name) + (is_dir ? 1 : 0) + 1; char *path = XMALLOC(MTYPE_TMP, path_len); // 直接拼接路径 if (base_path[0] != '\0') { snprintf(path, path_len, "%s%s%s", base_path, entry->d_name, is_dir ? "/" : ""); } else { snprintf(path, path_len, "%s%s", entry->d_name, is_dir ? "/" : ""); } printf("Found match: %s\n", path); temp_matches[count++] = path; } closedir(dir); // 没有匹配项 if (count == 0) { printf("No matches found\n"); return NULL; } // 排序匹配项 qsort(temp_matches, count, sizeof(char *), compare_strings); // 复制到状态 comp_state.matches = XCALLOC(MTYPE_TMP, (count + 1) * sizeof(char *)); for (int i = 0; i < count; i++) { comp_state.matches[i] = temp_matches[i]; } comp_state.match_count = count; comp_state.current_index = 0; printf("Initialized with %d matches\n", count); } // 返回匹配项或NULL if (comp_state.current_index < comp_state.match_count) { char *match = comp_state.matches[comp_state.current_index]; char *result = XSTRDUP(MTYPE_TMP, match); printf("Returning match[%d/%d]: %s\n", comp_state.current_index, comp_state.match_count, result); comp_state.current_index++; return result; } // 重置索引以便循环 printf("Resetting index for next cycle\n"); comp_state.current_index = 0; // 返回NULL表示结束 return NULL; } <dahua>dir e cmlsh_completion: enter----start = 4, end = 5 filename_completion_function: state=0 text=e Initializing new completion for: e Resetting completion state Set base_text: e No slash: base_path='', prefix='e' Opening directory: . Scanning directory entries... Found match: etc/ Initialized with 1 matches Returning match[0/1]: etc/ filename_completion_function: state=1 text=e Resetting index for next cycle filename_completion_matches: Found 1 matches for 'e': [0] etc/ tc/ cmlsh_completion: enter----start = 4, end = 8 filename_completion_function: state=0 text=etc/ Initializing new completion for: etc/ Resetting completion state Set base_text: etc/ Parsed: base_path='etc/', prefix='' Opening directory: etc/ Scanning directory entries... Found match: etc/ssh/ Found match: etc/nos.conf Found match: etc/CML_DB.db Initialized with 3 matches Returning match[0/3]: etc/CML_DB.db filename_completion_function: state=1 text=etc/ Returning match[1/3]: etc/nos.conf filename_completion_function: state=2 text=etc/ Returning match[2/3]: etc/ssh/ filename_completion_function: state=3 text=etc/ Resetting index for next cycle filename_completion_matches: Found 3 matches for 'etc/': [0] etc/CML_DB.db [1] etc/nos.conf [2] etc/ssh/ CML_DB.db cmlsh_completion: enter----start = 4, end = 17 filename_completion_function: state=0 text=etc/CML_DB.db Initializing new completion for: etc/CML_DB.db Resetting completion state Set base_text: etc/CML_DB.db Parsed: base_path='etc/', prefix='CML_DB.db' Opening directory: etc/ Scanning directory entries... Found match: etc/CML_DB.db Initialized with 1 matches Returning match[0/1]: etc/CML_DB.db filename_completion_func Username:
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值