📄 patternmodel.java
字号:
if ((caseSensitive) && ((flags & flag) != 0)) { pattern = Pattern.compile(pattern.pattern(), 0); } else if (!caseSensitive && ((flags & flag) == 0)) { pattern = Pattern.compile(pattern.pattern(), flag); } firePropertyChange("pattern", old, getPattern()); } public void addPropertyChangeListener(PropertyChangeListener l) { if (propertySupport == null) { propertySupport = new PropertyChangeSupport(this); } propertySupport.addPropertyChangeListener(l); } public void removePropertyChangeListener(PropertyChangeListener l) { if (propertySupport == null) return; propertySupport.removePropertyChangeListener(l); } protected void firePropertyChange(String name, Object oldValue, Object newValue) { if (propertySupport == null) return; propertySupport.firePropertyChange(name, oldValue, newValue); } /** * Responsible for converting a "raw text" into a valid * regular expression in the context of a set of rules. * */ public static class RegexCreator { protected String matchRule; private List rules; public String getMatchRule() { if (matchRule == null) { matchRule = getDefaultMatchRule(); } return matchRule; } public boolean isAutoDetect() { return false; } public String createRegEx(String searchString) { if (MATCH_RULE_CONTAINS.equals(getMatchRule())) { return createContainedRegEx(searchString); } if (MATCH_RULE_EQUALS.equals(getMatchRule())) { return createEqualsRegEx(searchString); } if (MATCH_RULE_STARTSWITH.equals(getMatchRule())){ return createStartsAnchoredRegEx(searchString); } if (MATCH_RULE_ENDSWITH.equals(getMatchRule())) { return createEndAnchoredRegEx(searchString); } return searchString; } protected String createEndAnchoredRegEx(String searchString) { return Pattern.quote(searchString) + "$"; } protected String createStartsAnchoredRegEx(String searchString) { return "^" + Pattern.quote(searchString); } protected String createEqualsRegEx(String searchString) { return "^" + Pattern.quote(searchString) + "$"; } protected String createContainedRegEx(String searchString) { return Pattern.quote(searchString); } public void setMatchRule(String category) { this.matchRule = category; } protected String getDefaultMatchRule() { return MATCH_RULE_CONTAINS; } public List getMatchRules() { if (rules == null) { rules = createAndInitRules(); } return rules; } private List createAndInitRules() { if (!supportsRules()) return Collections.EMPTY_LIST; List<String> list = new ArrayList<String>(); list.add(MATCH_RULE_CONTAINS); list.add(MATCH_RULE_EQUALS); list.add(MATCH_RULE_STARTSWITH); list.add(MATCH_RULE_ENDSWITH); return list; } private boolean supportsRules() { return true; } } /** * Support for anchored input. * * PENDING: NOT TESTED - simply moved! * Need to define requirements... * */ public static class AnchoredSearchMode extends RegexCreator { public boolean isAutoDetect() { return true; } public String createRegEx(String searchExp) { if (isAutoDetect()) { StringBuffer buf = new StringBuffer(searchExp.length() + 4); if (!hasStartAnchor(searchExp)) { if (isStartAnchored()) { buf.append("^"); } } //PENDING: doesn't escape contained regex metacharacters... buf.append(searchExp); if (!hasEndAnchor(searchExp)) { if (isEndAnchored()) { buf.append("$"); } } return buf.toString(); } return super.createRegEx(searchExp); } private boolean hasStartAnchor(String str) { return str.startsWith("^"); } private boolean hasEndAnchor(String str) { int len = str.length(); if ((str.charAt(len - 1)) != '$') return false; // the string "$" is anchored if (len == 1) return true; // scan backwards along the string: if there's an odd number // of backslashes, then the last escapes the dollar and the // pattern is not anchored. if there's an even number, then // the dollar is unescaped and the pattern is anchored. for (int n = len - 2; n >= 0; --n) if (str.charAt(n) != '\\') return (len - n) % 2 == 0; // The string is of the form "\+$". If the length is an odd // number (ie, an even number of '\' and a '$') the pattern is // anchored return len % 2 != 0; } /** * returns true if the pattern must match from the beginning of the string, * or false if the pattern can match anywhere in a string. */ public boolean isStartAnchored() { return MATCH_RULE_EQUALS.equals(getMatchRule()) || MATCH_RULE_STARTSWITH.equals(getMatchRule()); } //// /**// * sets the default interpretation of the pattern for strings it will later// * be given. Setting this value to true will force the pattern to match from// * the beginning of tested strings. Setting this value to false will allow// * the pattern to match any part of a tested string.// */// public void setStartAnchored(boolean startAnchored) {// boolean old = isStartAnchored();// this.startAnchored = startAnchored;// updatePattern(createRegEx(getRawText()));// firePropertyChange("startAnchored", old, isStartAnchored());// } // /** * returns true if the pattern must match from the beginning of the string, * or false if the pattern can match anywhere in a string. */ public boolean isEndAnchored() { return MATCH_RULE_EQUALS.equals(getMatchRule()) || MATCH_RULE_ENDSWITH.equals(getMatchRule()); } //// /**// * sets the default interpretation of the pattern for strings it will later// * be given. Setting this value to true will force the pattern to match the// * end of tested strings. Setting this value to false will allow the pattern// * to match any part of a tested string.// */// public void setEndAnchored(boolean endAnchored) {// boolean old = isEndAnchored();// this.endAnchored = endAnchored;// updatePattern(createRegEx(getRawText()));// firePropertyChange("endAnchored", old, isEndAnchored());// } //// public boolean isStartEndAnchored() {// return isEndAnchored() && isStartAnchored();// }// // /**// * sets the default interpretation of the pattern for strings it will later// * be given. Setting this value to true will force the pattern to match the// * end of tested strings. Setting this value to false will allow the pattern// * to match any part of a tested string.// */// public void setStartEndAnchored(boolean endAnchored) {// boolean old = isStartEndAnchored();// this.endAnchored = endAnchored;// this.startAnchored = endAnchored;// updatePattern(createRegEx(getRawText()));// firePropertyChange("StartEndAnchored", old, isStartEndAnchored());// } } /** * * @param mode */ public void setRegexCreatorKey(String mode) { if (getRegexCreatorKey().equals(mode)) return; String old = getRegexCreatorKey(); regexCreatorKey = mode; firePropertyChange("regexCreatorKey", old, getRegexCreatorKey()); } public String getRegexCreatorKey() { if (regexCreatorKey == null) { regexCreatorKey = getDefaultRegexCreatorKey(); } return regexCreatorKey; } private String getDefaultRegexCreatorKey() { return REGEX_MATCH_RULES; } public void setMatchRule(String category) { if (getMatchRule().equals(category)) { return; } String old = getMatchRule(); getRegexCreator().setMatchRule(category); updatePattern(createRegEx(getRawText())); firePropertyChange("matchRule", old, getMatchRule()); } public String getMatchRule() { return getRegexCreator().getMatchRule(); } private RegexCreator getRegexCreator() { if (regexCreator == null) { regexCreator = new RegexCreator(); } return regexCreator; } public List getMatchRules() { return getRegexCreator().getMatchRules(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -