Reputation: 95
I have a string like this :
002_part1_part2_______________by_test
and I would like to stop the match at the second underscore character, like this :
002_part1_part2_
How can I do that with a Regular expression ?
Thanks
Upvotes: 1
Views: 24478
Reputation: 99071
You can use:
.*\d_
EXPLANATION:
Match any single character that is NOT a line break character (line feed) «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match a single character that is a “digit” (any decimal number in any Unicode script) «\d»
Match the character “_” literally «_»
https://regex101.com/r/uX0qD5/1
Upvotes: 0
Reputation: 174874
Create a pattern to match any character but not of an _
zero or more times followed by an underscore symbol. Put that pattern inside a capturing or non-capturing group and make it to repeat exactly 3 times by adding range quantifier {3}
next to that group.
^(?:[^_]*_){3}
Upvotes: 1