How to delete text between [...]

A swapping-ground for Regular Expression syntax

How to delete text between [...]

Postby dabiel » Tue Feb 23, 2010 10:24 pm

For example, I have many files like:

My document 1 [created by John].doc
Order of US [Peter and Jim].xls

What I want is to rename them to:

My document 1.doc
Order of US.xls

I am trying to sue the Regular Expressions, but no luck, it is more hard that I expected.

Is there a way to do it?

Thank you
dabiel
 
Posts: 1
Joined: Tue Feb 23, 2010 10:11 pm

Re: How to delete text between [...]

Postby Stefan » Wed Feb 24, 2010 9:53 am

dabiel wrote:For example, I have many files like:

My document 1 [created by John].doc
Order of US [Peter and Jim].xls

What I want is to rename them to:

My document 1.doc
Order of US.xls

I am trying to sue the Regular Expressions, but no luck, it is more hard that I expected.

Is there a way to do it?

Thank you
Hi dabiel and welcome.

FROM:
My document 1 [created by John].doc
Order of US [Peter and Jim].xls
TO:
My document 1.doc
Order of US.xls

DO:
- to find an solution, virtual split your string into parts. e.g.:
"My document 1"
" "
"["
"created by John"
"]"

Now we see we have to search for
(one-or-more of any sign),
till an blank
followed by an '['.
Then one-or-more of any sign,
followed by ']'

So we search for all till an blank followed by an '[' , capture that in an () group and use this as replacement, dropping the rest of the file name.

With that knowing, the solution would be :
RegEx(1)
Match: (.+) [.+]
Repla: \1

But since '[' and ']' are RegEx meta signs, you have to escape them by an back slash like : '\[' and '\]'

So the real solution is:
RegEx(1)
Match: (.+) \[.+\]
Repla: \1


---------------------------

General notes/ Disclaimer

Hope this helps ? :D

Please note:

* Test my solution first with some test files before you destroy your data.

* It's always an good idea to provide all possibilities of file name pattern in question.
* That would give the supporter an change to do it right ;)
* If your real file names doesn't fit into your example pattern my solution may fail.

* Don't use this ' ' or " " -quotes from my explanation. They are only for clarification.
* '?' means non-greedy: stop at first match, instead of last possible.
* This (...) parenthesis are used to "group" what is found by this RegEx inside the ()'s to reuse this capture later for replacement.
* Instead of ~ -signs, if used in my explanations, type an space/blank.


More Help
* online tester:
- http://rereplace.com/
- http://www.regextester.com/
- http://www.regexlib.com/RETester.aspx

* online help:
- www.regular-expressions.info
- www.regexlib.com/
- www.regexlib.com/CheatSheet.aspx

See this both oldest threads in the "Regular Expressions" forum for an RegEx syntax overview:
=> Getting Started: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=5
=> Go ahead: http://www.bulkrenameutility.co.uk/forum/viewtopic.php?f=3&t=27
There you will find more examples and tips as you may find in other threads in the "Regular Expressions" sub-forum.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions