StringModificatorWithRegex - Hauptthema

#1 von Falk , 27.10.2016 12:55

Dieses kleine Projekt soll die Auswertung von regulären Ausdrücken standardisieren, um bei der Nutzung von diesen Ausdrücken sich nur noch auf den Ausdruck selber und das Auswertungsergebnis zu konzentrieren.

Zusätzlich soll diese Auswertung bei der Modifikation von Strings genutzt werden.

Bald erscheint hier die erste Version.

Falk  
Falk
Beiträge: 23
Registriert am: 20.09.2016


RE: StringModificatorWithRegex - Hauptthema

#2 von Falk , 11.11.2016 10:10

Es existiert nun die Grundfunktion (Berechnung des Ergebnisses) in der ersten Version.

Hier nun der Code Version 0.1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 
class RegexSyntaxException(message: String) extends Throwable
 
case class Group(matching: String, startPositionMatching: Int, indexGroup: Int)
 
case class Full(matching: String, startPositionMatching: Int)
 
case class MatchResult(isMatching: Boolean, regexHasGroup: Boolean, isGroupMatching: Boolean, groups: Seq[Group], fulls: Seq[Full])
 

class StringModifierWithRegex(regex: String) {
private val regexCompiled = compile()
private var matchResult = MatchResult(isMatching = false, regexHasGroup = false, isGroupMatching = false, Seq[Group](), Seq[Full]())
 
private def compile() : Pattern = {
var result = Pattern.compile("")
 
try {
result = Pattern.compile(regex)
}
catch {
case ex: PatternSyntaxException => throw new RegexSyntaxException(ex.getMessage)
}
result
}
 
def evaluate(text: String) : MatchResult = {
val matcher = regexCompiled.matcher(text)
 
matchResult = fillResult(matcher)
matchResult
}
 
private def fillResult(matcher: Matcher) : MatchResult = {
var fulls = Seq[Full]()
var groups = Seq[Group]()
 
val regexHasGroup = if (matcher.groupCount() == 0) false else true
 
def findNextAndFillResultRec(matcher: Matcher, matchN: Int) : MatchResult = {
val findNext = matcher.find()
if (matchN == 1 && ! findNext) {
MatchResult(isMatching = false, regexHasGroup, isGroupMatching = false, Seq[Group](), Seq[Full]())
}
else if (findNext) {
// calculate result
val fullStartPosition = matcher.start()
val fullMatch = matcher.group()
 
fulls = fulls:+Full(fullMatch, fullStartPosition)
 
var groupStartPosition = 0
 
breakable {
for (i <- 1 to matcher.groupCount()) {
groupStartPosition = matcher.start(i)
if (groupStartPosition != -1) {
groups = groups:+Group(matcher.group(i), groupStartPosition, i)
break
}
}
}
 
findNextAndFillResultRec(matcher, matchN + 1)
}
// NOT (matcher.find())
else {
val isMatching = if (fulls.size > 0) true else false
val isGroupMatching = if (groups.size > 0) true else false
MatchResult(isMatching, regexHasGroup, isGroupMatching, groups, fulls)
}
}
 
findNextAndFillResultRec(matcher, 1)
}
}
 


Falk  
Falk
Beiträge: 23
Registriert am: 20.09.2016

zuletzt bearbeitet 11.11.2016 | Top

RE: StringModificatorWithRegex - Hauptthema

#3 von Falk , 12.11.2016 15:33

Hier nun die nächste Version mit ersetzen aller Fullmatches bzw. Groupmatches.

Version 0.2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
class RegexSyntaxException(message: String) extends Exception(message)
 
case class Group(matching: String, startPositionMatching: Int, indexGroup: Int)
 
case class Full(matching: String, startPositionMatching: Int)
 
case class MatchResult(isMatching: Boolean, regexHasGroup: Boolean, isGroupMatching: Boolean, groups: Seq[Group], fulls: Seq[Full])
 

class StringModifierWithRegex(regex: String) {
private val regexCompiled = compile()
 
def test(text: String) : MatchResult = {
val matcher = regexCompiled.matcher(text)
fillResult(matcher)
}
 
def replaceAllFullMatches(text: String, replacement: String) : String = {
var newText = text
var nextMatch: Option[(Full, Option[Group])] = None
var startIndex = 0
 
do {
val matcher = regexCompiled.matcher(newText)
nextMatch = findNextMatch(matcher, startIndex)
if (nextMatch != None) {
newText = replaceText(newText, nextMatch.get._1.startPositionMatching, nextMatch.get._1.matching.length, replacement)
startIndex = nextMatch.get._1.startPositionMatching + replacement.length
}
} while (nextMatch != None)
newText
}
 
def replaceAllGroupMatches(text: String, replacement: String) : String = {
var newText = text
var nextMatch: Option[(Full, Option[Group])] = None
var startIndex = 0
 
do {
val matcher = regexCompiled.matcher(newText)
nextMatch = findNextMatch(matcher, startIndex)
if (nextMatch != None && nextMatch.get._2 != None) {
newText = replaceText(newText, nextMatch.get._2.get.startPositionMatching, nextMatch.get._2.get.matching.length, replacement)
startIndex = nextMatch.get._2.get.startPositionMatching + replacement.length
}
} while (nextMatch != None)
newText
}
 
private def compile() : Pattern = {
var result = Pattern.compile("")
 
try {
result = Pattern.compile(regex)
}
catch {
case ex: PatternSyntaxException => throw new RegexSyntaxException(ex.getMessage)
}
result
}
 
private def replaceText(text: String, startPosition: Int, length: Int, replacement: String) : String = {
val before = text.substring(0, startPosition)
val after = text.substring(startPosition + length)
before + replacement + after
}
 
private def fillResult(matcher: Matcher) : MatchResult = {
var fulls = Seq[Full]()
var groups = Seq[Group]()
 
val regexHasGroup = if (matcher.groupCount() == 0) false else true
 
def findNextAndFillResultRec(matcher: Matcher, matchN: Int) : MatchResult = {
val findNext = matcher.find()
if (matchN == 1 && ! findNext) {
MatchResult(isMatching = false, regexHasGroup, isGroupMatching = false, Seq[Group](), Seq[Full]())
}
else if (findNext) {
// calculate result
val fullStartPosition = matcher.start()
val fullMatch = matcher.group()
 
fulls = fulls:+Full(fullMatch, fullStartPosition)
 
var groupStartPosition = 0
 
breakable {
for (i <- 1 to matcher.groupCount()) {
groupStartPosition = matcher.start(i)
if (groupStartPosition != -1) {
groups = groups:+Group(matcher.group(i), groupStartPosition, i)
break
}
}
}
 
findNextAndFillResultRec(matcher, matchN + 1)
}
// NOT (matcher.find())
else {
val isMatching = if (fulls.size > 0) true else false
val isGroupMatching = if (groups.size > 0) true else false
MatchResult(isMatching, regexHasGroup, isGroupMatching, groups, fulls)
}
}
 
findNextAndFillResultRec(matcher, 1)
}
 
def findNextMatch(matcher: Matcher, startIndex: Int) : Option[(Full, Option[Group])] = {
var resultFull = Full("", 0)
var resultGroup: Option[Group] = None
 
val findNext = matcher.find(startIndex)
if (! findNext) {
None
}
else {
// calculate result
val fullStartPosition = matcher.start()
val fullMatch = matcher.group()
 
resultFull = Full(fullMatch, fullStartPosition)
 
var groupStartPosition = 0
 
breakable {
for (i <- 1 to matcher.groupCount()) {
groupStartPosition = matcher.start(i)
if (groupStartPosition != -1) {
resultGroup = Some(Group(matcher.group(i), groupStartPosition, i))
break
}
}
}
 
Some(resultFull, resultGroup)
}
}
 
}
 


Falk  
Falk
Beiträge: 23
Registriert am: 20.09.2016

zuletzt bearbeitet 12.11.2016 | Top

   

StringModificatorWithRegex - Bestehende ähnliche Implementierungen

Xobor Einfach ein eigenes Xobor Forum erstellen
Datenschutz