Schauen ob ganze Zeichenkette aus Wiederholung von 3 Zeichen besteht? Regex?
Hi, wie kann ich per Regex schauen, ob eine Zeichenkette aus immer 3 Zeichen besteht, dei sich wiederholen. Dabei ist es egal welches Zeichen es ist z. B:
"-+#-+#-+#-+#" das wäre true, weil sich immer die gleichen drei Zeichen wiederholen oder
"111111111" auch true
"123123123123" auch true
"uuukkk" false
Wie geht das mit readex? Die Zeichen sind dabei egal, hautpsache immer drei gleiche.
3 Antworten
Noch eine bessere Lösung:
(...)\1*
Evtl. musst du die Backreference noch Klammern und für die Punkte kannst du auch direkt die Anzahl angeben:
(.{3})(\1)+
falsch
412123123
ergibt ein 2 match
(...)\1*
/
gm
1st Capturing Group (...)
. matches any character (except for line terminators)
. matches any character (except for line terminators)
. matches any character (except for line terminators)
\1 matches the same text as most recently matched by the 1st capturing group
* matches the previous token between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
^(.)(.)(.)(\1\2\3)+$
test
431123123 false
123123123 true
es gibt aber keine andere syntax, regex ist schon über 40 jahre alt .
wenn du etwas in " setzt weisst du aber das jegliches \ mit \ davor entwertet wird, weil "\n" wird ausgewertet als return bzw nächste zeile.
ergo ist \\ das zeichen \ , sonst wäre es ein steuerzeichen wie \n
du kannst auch ' nehmen dann brauchst du dir die mühe nicht machen , den das wird nicht ausgewertet
'^(.)(.)(.)(\1\2\3)+$'
oder halt
"^(.)(.)(.)(\\1\\2\\3)+$"
(.)(.)(.)(\1\2\3)+
Danke, könntest Du vielleicht erklären, was die Erklärung dahinter ist, also warum das so funktioniert? Die Bedeutung der Punkte sind mir klar, ich mein eher die{\1\2\3
Danke dir, weißt Du zufällig wie man das in Java umschreibt? Also wie in etwa deer Code für Java aussieht?
falsch
431123123
ergibt ein match
/
(.)(.)(.)(\1\2\3)+
/
gm
1st Capturing Group (.)
. matches any character (except for line terminators)
2nd Capturing Group (.)
. matches any character (except for line terminators)
3rd Capturing Group (.)
. matches any character (except for line terminators)
4th Capturing Group (\1\2\3)+
+ matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
\1 matches the same text as most recently matched by the 1st capturing group
\2 matches the same text as most recently matched by the 2nd capturing group
\3 matches the same text as most recently matched by the 3rd capturing group
Global pattern flags
g modifier: global. All matches (don't return after first match)
m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Danke, online habe ich das so auch mal hinbekommen. Problem ist Java hat diese Syntex mit einem Beispiel:
([aB]{1,2})\\1
Das führt beispielsweise bei
aa true
aB false
aaBB false
aBaB true
kannst Du vllt. deinen Code umrüsten zu der Syntax? ich bekomme das irgendwie nicht hin