Remove all chars after x occurrences of a char - RegEx?

A swapping-ground for Regular Expression syntax

Remove all chars after x occurrences of a char - RegEx?

Postby gritsintexas » Mon Dec 05, 2016 2:42 pm

Pretty sure I need to use RegEx for this task, but trying to figure out RegEx is putting hair on my chest. I have a slew (+55K) of pdf files that have a format of:
SEQ1_SEQ2_SEQ3_SEQ4.pdf

I need to remove the underscore ("_") before SEQ3 and everything to the right of it. A successful result would look like:
SEQ1_SEQ2.pdf

Problem is that SEQ1 and SEQ2 having varying lengths, so I can't key off those. Basically need to dump everything from the 2nd "_" on.

Any assistance would be greatly appreciated.
gritsintexas
 
Posts: 2
Joined: Mon Dec 05, 2016 1:33 pm

Re: Remove all chars after x occurrences of a char - RegEx?

Postby KenP » Mon Dec 05, 2016 3:26 pm

This will leave everything to the left of the second underscore.

RegEx (1)
Match: ^([^_]*_[^_]*)
Replace: \1
KenP
 
Posts: 199
Joined: Sat Jul 30, 2016 11:25 am

Re: Remove all chars after x occurrences of a char - RegEx?

Postby gritsintexas » Mon Dec 05, 2016 3:39 pm

Uh, huzzah KenP! BRU is a powerful tool that you clearly dominate. I obviously need to spend more quiet time with it.

Thank you for the quick assist.
gritsintexas
 
Posts: 2
Joined: Mon Dec 05, 2016 1:33 pm

Re: Remove all chars after x occurrences of a char - RegEx?

Postby therube » Tue Dec 06, 2016 1:20 am

I had to think that one through a bit.
Thanks Ken.

I believe this should do the same:

1:RegEx
Code: Select all
Match: (.*?)_(.*?)_(.*)
Replae: \1_\2
therube
 
Posts: 1314
Joined: Mon Jan 18, 2016 6:23 pm


Return to Regular Expressions