Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to truncate last 4 characters and make sure it doesn't contain specific words

I get strings such as "BumpCardV2Resource.getInstance.Time" or "BumpCardWebResource.getInstance.Time" or "BumpCardResource.getInstance.Time". I need a regex expression to obtain only "BumpCardResource.getInstance"

Currently, I’m using a negative look ahead to make sure the string does not contain V2 or Web but not sure how to truncate the last 5 characters (.Time) along with that.

Regex I’m using: /^(?!.*V2|.*WebResource).*$/

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PS – The resource and the API endpoint keeps changing. It need not be necessarily only BumpCard or getInstance

>Solution :

You can use a regex where you match the whole string and capture any text from the start of the string till .Time at the end of it:

/^(?!.*V2|.*WebResource)(.*)\.Time$/

See the regex demo. Details:

  • ^ – start of string
  • (?!.*V2|.*WebResource) – no V2 or WebResource allowed anywhere in the string
  • (.*)Capturing group 1: any zero or more chars other than line break chars as many as possible
  • \.Time.Time string
  • $ – end of string.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading