• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

GREP for 1st, 2nd, 3rd, 4th

Community Beginner ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

Hello all,

I'm not so hot with GREP, but I love using it for formating text when I can.

I'd like to apply small caps to "st, nd, rd and th" when used in 1st, 2nd, 3rd, 4th, etc., but I'm having trouble limiting where it is applied. I've tried some positive look behind and look ahead rules, but I'm still failing.

 

I'm using the GREP below:

(?<=\d)th|nd|rd|st(?=.+?)

 

Using it for addressing like below:

151 West 34th Street 

27th Fl.

Long Island City, NY 11101

 

I don't want the "st" in West or the "nd" in Island small affected, but haven't figured out how to control that. Any suggestions greatly appreciated.

 

Thanks,

Dennis

TOPICS
Print , Scripting

Views

335

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Dec 09, 2022 Dec 09, 2022

Hi @dennisy42803903, I'm no expert but I'm a huge fan of grep, too 🙂

You could try this:

 

(?<=\d)(th|nd|rd|st)\b

 

 I put parenthesis around the OR choices and added \b (word boundary).

- Mark

Votes

Translate

Translate
Community Expert ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

Hi @dennisy42803903, I'm no expert but I'm a huge fan of grep, too 🙂

You could try this:

 

(?<=\d)(th|nd|rd|st)\b

 

 I put parenthesis around the OR choices and added \b (word boundary).

- Mark

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

Hey Mark,

Thanks for the quick and effective reply. It definitely seems to work. I have to apply this to several hundred addresses, so hopefully I won't find any exceptions. However, first 30 examples look promising. 

I really appreciate the help. I never thought to use a word boundary like that. I'll add that to my arsenal for next time.

Thank you!

-Dennis

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 09, 2022 Dec 09, 2022

Copy link to clipboard

Copied

LATEST

Yeah the word boundary was to add a little more robustness against matching, say, a wacky start-up company name "12think". Adding the parenthesis to group options separated by bar (|) is almost always essential. In your original grep, I think the lookahead at the end would only apply to "st".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines