You are viewing limited content. For full access, please sign in.

Question

Question

regular expression for character that may or may not be there

asked on March 10, 2016 • Show version history

I want to capture the first 4-7 characters of a file name that come before an underscore. My current expression looks like: (\S{4,7})_

 

This parses out: ACCEL_Certificates of Coverage to just ACCEL, which is what I want.

 

However, I also want this same regular expression to accept just the file name ACCEL, with no underscore. How do I construct this to accept a file name with an underscore or without an underscore? Everything I have tried either gives me nothing or will pull ACCEL_C. I know this should be so simple, but I'm just not figuring it out. Thanks!

0 0

Answer

SELECTED ANSWER
replied on March 10, 2016

Try ^([^_]{4,7})

This is matching anything that's not an underscore starting at the beginning of the string, at least 4 characters in a row, no more than 7. Once it hits an underscore, it will stop.

1 0

Replies

replied on March 10, 2016

If there is no underscore, how do you tell where that value ends? Is there a space or can it look like ACCELCertificates of Coverage?

0 0
replied on March 10, 2016 • Show version history

It would generally just be "ACCEL" - with no space, no underscore, no nothing.

 

It was originally designed to take files from our website that have a certain naming convention and contain an underscore.

 

We also want to use it to file new documents, so I have users that just need to name the document ACCEL, ALAME, just the first 5 characters (or sometimes 4 or 7), that are required to batch file documents - this identifies which member it belongs to, so that can be assigned using a lookup, where all other metadata for the batch of files is the same.

 

However, they currently have to name them ACCEL_ for the workflow to recognize them. They have trouble remembering to put the underscore in there, and I would prefer that the workflow be able to accept either or.

0 0
SELECTED ANSWER
replied on March 10, 2016

Try ^([^_]{4,7})

This is matching anything that's not an underscore starting at the beginning of the string, at least 4 characters in a row, no more than 7. Once it hits an underscore, it will stop.

1 0
replied on March 10, 2016

Thank you, Miruna! You are the best!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.