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

RegEx pattern in chapters numbers

Contributor ,
Sep 28, 2022 Sep 28, 2022

Copy link to clipboard

Copied

Hi friends,

 

Please help on this.

I need to catch the below all chapters in the RegEx pattern.

 

I have try the this pattern - ((C|c)hapter(s)?[\s][0-9–]+[\s\,0-9&]*[and]{3,3}[\s0-9]+)|((C|c)hapter(s)?[\s][0-9–]+[\s\,0-9&]*) - not working properly.

 

Chapter 1

Chapters 1–6

Chapters 2 and 3

Chapters 2 & 3

Chapters 2, 3, 4

Chapters 2, 3, and 4

Chapters 2, 3 and 4

 

Thanks in Advance,

Karthik S

TOPICS
Scripting

Views

571

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 2 Correct answers

Community Expert , Sep 29, 2022 Sep 29, 2022

Right. How about this:

([Cc]hapter\s\d+|[Cc]hapters?\s([\d,\s\-–]|(and|\&)\s\d+)+)

- Mark 

Votes

Translate

Translate
Community Expert , Sep 29, 2022 Sep 29, 2022

Nice one, Mark. For interest's sake, just a couple of small things. Your expression matches the space characters that follow numbers. And when you make optional whatever follows a single number, there's no need to repeat 'Chapter':

[Cc]hapters?\s\d+  // This is the minimum match
(((–|,\s|,?\s(&|and)\s)\d+)+)?   // Followed by any of these

 Peter

Votes

Translate

Translate
Community Expert ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Hi @karthikS, here's what I came up with...

[Cc]hapters?\s\d+[,\-–\s]+(and|\&)?(\s?\d+[,\-–\s]+(and|\&)?)?\s?\d*

It's a bit of a brute force approach, but worked in my simple testing.

- 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
Contributor ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Hi Mark,

Thank you very much! the above pattern wrong catch some cases. It's possible to ignore this case?

 

1. chapter 1 and ==> (ingnore [and space character])

2. chapter 1, ==> (ingnore [comma space character])

3. Chapters 2, 3 and ==> (ingnore [and space character])

4. Chapter 1 & ==> (ingnore [& space character])

 

Thanks in Advance,

Karthik S

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Right. How about this:

([Cc]hapter\s\d+|[Cc]hapters?\s([\d,\s\-–]|(and|\&)\s\d+)+)

- 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
Contributor ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Hi Mark,

 

Thanks lot! very good job

Working fine

- karthik S

 

 

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Great!

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Nice one, Mark. For interest's sake, just a couple of small things. Your expression matches the space characters that follow numbers. And when you make optional whatever follows a single number, there's no need to repeat 'Chapter':

[Cc]hapters?\s\d+  // This is the minimum match
(((–|,\s|,?\s(&|and)\s)\d+)+)?   // Followed by any of these

 Peter

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Excellent! Thanks Peter. 🙂

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Peter, look how beautiful your REGEX is !!  🙂Screen 1.png

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Thank you, Jean-Claude. And what a lovely and instructive graphic! How did you do that?

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

You can get a visual representation of any Regex from one of theses web site:
https://lehollandaisvolant.net/tout/tools/regex/
or
https://www.debuggex.com/

They are not fully compatible with all the InDesign GREP flavor, but for a visual guy like me I found them pretty useful.

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

Very nice tools, thank you.

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 ,
Sep 29, 2022 Sep 29, 2022

Copy link to clipboard

Copied

LATEST

Thanks Jean-Claude, very helpful.

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