Reputation: 7500
If I have an example string as follows:
LEARNING-LEARNING-LEARNING-LEARNING-LEARNING-COMPANY_ORG_CHART-LEARNING-LEARNING-LEARNING-END
I want to extract only the first n no. of steps where n is defined by the no. of hyphens in between.
So if n=5,
I want only the first 5 steps.
Output:
LEARNING-LEARNING-LEARNING-LEARNING-LEARNING
I tried this:
s1=re.search(r'([A-za-z_].*-{0,5}[A-za-z_].*?)',s)
print(s1.group())
But it is giving whole string as output.
LEARNING-LEARNING-LEARNING-LEARNING-LEARNING-COMPANY_ORG_CHART-LEARNING-LEARNING-LEARNING-END
In [ ]:
Upvotes: 0
Views: 95
Reputation: 11346
Give this a try:
s1 = re.search(r'^([A-za-z_]+-){0,4}[A-za-z_]+',s)
print(s1)
(this example is for n=5)
Upvotes: 0
Reputation: 5955
If this is the actual format, then regex may be overkill for the sample provided. One other option is just using string methods and indexing:
x='LEARNING-LEARNING-LEARNING-LEARNING-LEARNING-COMPANY_ORG_CHART-LEARNING-LEARNING-LEARNING-END'
print('-'.join(x.split('-')[:5]))
'LEARNING-LEARNING-LEARNING-LEARNING-LEARNING'
Upvotes: 6