asenovm
asenovm

Reputation: 6517

Checking whether some text consists only of some pattern

I have the following problem: I'm trying to check whether some text consists only of a number of repetitions of some pattern. I.e I have a 1000 lines text and want to check if it consists only of

asd
123

I tried matching not the pattern, but (pattern)+ hoping that it will match whatever there is to match but it was to no avail. My other idea was to split the string with the text upon the regex, but it didn't work either. I'm writing this using python re module if it matters. Thanks!

Upvotes: 2

Views: 109

Answers (3)

Codeman
Codeman

Reputation: 12375

I haven't tried this in Python, but the regex for what you are asking for would be the following:

.asd$
.123$

Try reading through the following webpage and see if it gives you a better understanding:

http://www.regular-expressions.info/reference.html

Hope this helps!

Upvotes: 1

Bohemian
Bohemian

Reputation: 425033

Perhaps you meant: ^(abc|123)+$

Upvotes: 1

Qtax
Qtax

Reputation: 33908

Try matching the string with ^(?:asd|123)+$, if it matches, then it only contains combinations of asd or 123 (at least one).

Upvotes: 1

Related Questions