Need regular expression that will work to find numeric and alpha characters in a string -
here's i'm trying do. user can type in search string, can include '*' or '?' wildcard characters. i'm finding works regular strings not ones including numeric characters.
e.g: 414d512052524d2e535441524b2e4e45298b8751202ae908 1208
if section of hex string, returns false. if "120" or "208" in "1208" string fails.
right now, regular expression pattern ends looking when user enters, "w?f": '\bw.?f\b'
i'm (obviously) not well-versed in regular expressions @ moment, appreciate pointers may have handle numeric characters in way need - thanks!
code in question:
/** * * @param searchstring * @param strtobesearched * @return */ public boolean findstring(string searchstring, string strtobesearched) { pattern pattern = pattern.compile(wildcardtoregex(searchstring)); return pattern.matcher(strtobesearched).find(); } private string wildcardtoregex(string wildcard){ stringbuffer s = new stringbuffer(wildcard.length()); s.append("\\b"); (int = 0, = wildcard.length(); < is; i++) { char c = wildcard.charat(i); switch(c) { case '*': s.append(".*"); break; case '?': s.append(".?"); break; default: s.append(c); break; } } s.append("\\b"); return(s.tostring()); }
let's assume string search in is
1208 the search "term" user enters is
120 the pattern is
\b120\b the \b (word boundary) meta-character matches beginning , end of "words".
in our example, can't work because 120 != 1208
the pattern has be
\b.*120.*\b where .* means match variable number of characters (including null).
solution:
- either add
.*swildcardtoregex(...)method make functionality work out-of-the-box, - or tell users search
*120*, because*wildcard character same.
is, in fact, preference because user can define whether search entries starting (searchsomething*), including (*something*), ending (*something), or (something).
Comments
Post a Comment