Change one Group to Capital letters?

A swapping-ground for Regular Expression syntax

Change one Group to Capital letters?

Postby violent.peach » Fri Feb 14, 2020 8:55 pm

I'm trying to change a bunch of initials to uppercase from lower case letters. I have created a match that puts those letters in its own group. Is there a way to use that to change only that group to upper case letters?

Here is my match: (^\d*.\s\w*,\s)(\w\w)(,\s.*)

Below is an example of the titles I am trying to change. The portion I want to be capitalized is underline.
377. Marco, Ml, et al, Health Benefits of Fermented Foods Microbiota and Beyond Current Opin ion in Biotechnology, 2017 44 P 94-102
378. Lucey, Ja, Raw Milk Consumption Risks and Benefits Nutrition Today, 2015 50(4) P 189-193

I would be very grateful for help!
violent.peach
 
Posts: 1
Joined: Fri Feb 14, 2020 8:48 pm

Re: Change one Group to Capital letters?

Postby RegexNinja » Fri Jun 05, 2020 7:43 am

Unfortunately, BRU's regex doesnt support case-conversion in the replacement.
But with the paid version, javascript easily handles it:

Method1:
newName=name.replace(/^(\d{2,}\. [A-Z].+?, [A-Z])([a-z])(,.*)$/, function fix(abc,a,b,c) {return [ a + b.toUpperCase() + c ]})

Method2:
if (/^\d{2,}\. [A-Z].+?, [A-Z][a-z],.*$/.test(name)) {
begin = name.replace(/^(\d{2,}\. [A-Z].+?, [A-Z]).*$/, '$1')
fix = name.replace(/^\d{2,}\. [A-Z].+?, [A-Z]([a-z]).*$/, '$1')
end = name.replace(/^\d{2,}\. [A-Z].+?, [A-Z][a-z](,.*)$/, '$1')
newName = begin + fix.toUpperCase() + end }

Results:
377. Marco, Ml, Anytext ----> 377. Marco, ML, Anytext
378. Lucey, Ja, Anytext -----> 378. Lucey, JA, Anytext
RegexNinja
 
Posts: 134
Joined: Fri Feb 21, 2020 5:26 pm


Return to Regular Expressions