PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/utils/RegularExpression.java

#
Java | 124 lines | 93 code | 28 blank | 3 comment | 8 complexity | 24b8e5440e75200b20dd5b652d6b2156 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. package org.jedit.ruby.utils;
  2. import java.util.regex.Pattern;
  3. import java.util.regex.MatchResult;
  4. import java.util.regex.Matcher;
  5. import java.util.List;
  6. import java.util.ArrayList;
  7. /**
  8. * @author robmckinnon at users,sourceforge,net
  9. */
  10. public class RegularExpression {
  11. private Pattern pattern;
  12. public RegularExpression() {
  13. initPattern(getPattern());
  14. }
  15. public RegularExpression(String pattern) {
  16. initPattern(pattern);
  17. }
  18. private void initPattern(String pattern) {
  19. this.pattern = Pattern.compile(pattern);
  20. }
  21. public boolean hasMatch(String text) {
  22. return pattern.matcher(text).find();
  23. }
  24. public boolean isMatch(String text) {
  25. return pattern.matcher(text).matches();
  26. }
  27. protected String getPattern() {
  28. throw new IllegalStateException("override to use");
  29. }
  30. public MatchResult firstMatch(String line) {
  31. Matcher matcher = pattern.matcher(line);
  32. if (matcher.find()) {
  33. return new MatchResultWithoutNullGroups(matcher.toMatchResult());
  34. } else {
  35. return null;
  36. }
  37. }
  38. public MatchResult lastMatch(String line, int startGroupIndex) {
  39. Matcher matcher = pattern.matcher(line);
  40. MatchResult match = null;
  41. int start = 0;
  42. while (matcher.find(start)) {
  43. match = matcher.toMatchResult();
  44. start = match.start(startGroupIndex);
  45. }
  46. return match == null ? null : new MatchResultWithoutNullGroups(match);
  47. }
  48. public MatchResult[] getAllMatchResults(String line) {
  49. Matcher matcher = pattern.matcher(line);
  50. List<MatchResult> matches = new ArrayList<MatchResult>();
  51. while (matcher.find()) {
  52. matches.add(matcher.toMatchResult());
  53. }
  54. return matches.toArray(new MatchResult[matches.size()]);
  55. }
  56. public int allMatchResults(String line) {
  57. int count = 0;
  58. Matcher matcher = pattern.matcher(line);
  59. if (matcher.matches()) {
  60. boolean nextMatch = matcher.find(0);
  61. while (nextMatch) {
  62. count++;
  63. nextMatch = matcher.find(matcher.end());
  64. }
  65. }
  66. return count;
  67. }
  68. private static class MatchResultWithoutNullGroups implements MatchResult {
  69. private final MatchResult result;
  70. public MatchResultWithoutNullGroups(MatchResult result) {
  71. this.result = result;
  72. }
  73. public int start() {
  74. return result.start();
  75. }
  76. public int start(int group) {
  77. return result.start(group);
  78. }
  79. public int end() {
  80. return result.end();
  81. }
  82. public int end(int group) {
  83. return result.end(group);
  84. }
  85. public String group() {
  86. return result.group();
  87. }
  88. public String group(int group) {
  89. String match = result.group(group);
  90. return match == null ? "" : match;
  91. }
  92. public int groupCount() {
  93. return result.groupCount();
  94. }
  95. }
  96. }