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

RegEx pattern in chapters numbers

Contributor ,
Sep 28, 2022 Sep 28, 2022

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
975
Translate
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 

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

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

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 

Translate
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

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

Translate
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

Right. How about this:

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

- Mark 

Translate
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

Hi Mark,

 

Thanks lot! very good job

Working fine

- karthik S

 

 

Translate
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

Great!

Translate
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

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

Translate
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

Excellent! Thanks Peter. 🙂

Translate
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

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

Translate
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

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

Translate
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

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.

Translate
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

Very nice tools, thank you.

Translate
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
LATEST

Thanks Jean-Claude, very helpful.

Translate
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