Schauen ob ganze Zeichenkette aus Wiederholung von 3 Zeichen besteht? Regex?

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)+
TechPech1984  06.11.2021, 21:08

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)

0
^(.)(.)(.)(\1\2\3)+$

test

431123123 false

123123123 true

aisdqw8 
Fragesteller
 06.11.2021, 21:14

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

0
TechPech1984  06.11.2021, 21:22
@aisdqw8

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)+$"
0
(.)(.)(.)(\1\2\3)+
aisdqw8 
Fragesteller
 06.11.2021, 20:59

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

1
Apeirogon  06.11.2021, 21:02
@aisdqw8

Das nennt man "Backreferences".

Ein Backslash gefolgt von einer Ziffer entspricht dem Inhaltt in den Klammern von Links. Gezählt wird nicht von 0, sondern von 1 an.

1
aisdqw8 
Fragesteller
 06.11.2021, 21:04
@Apeirogon

Danke dir, weißt Du zufällig wie man das in Java umschreibt? Also wie in etwa deer Code für Java aussieht?

0
Apeirogon  06.11.2021, 21:07
@aisdqw8

Backslashes verdoppeln:

String regex = "(...)(\1)*";
0
aisdqw8 
Fragesteller
 06.11.2021, 21:18
@Apeirogon

Danke dir, was hei0t blackslahes verdoppeln?

0
Apeirogon  06.11.2021, 21:26
@aisdqw8

Aus \ einfach \\ machen:

String regex = "^(...)\\1*$";

Sorry, hatte mich davor vertippt. :)

1
TechPech1984  06.11.2021, 21:02

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)

0