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
Right. How about this:
([Cc]hapter\s\d+|[Cc]hapters?\s([\d,\s\-–]|(and|\&)\s\d+)+)
- Mark
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
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
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
Copy link to clipboard
Copied
Right. How about this:
([Cc]hapter\s\d+|[Cc]hapters?\s([\d,\s\-–]|(and|\&)\s\d+)+)
- Mark
Copy link to clipboard
Copied
Hi Mark,
Thanks lot! very good job
Working fine
- karthik S
Copy link to clipboard
Copied
Great!
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
Copy link to clipboard
Copied
Excellent! Thanks Peter. 🙂
Copy link to clipboard
Copied
Peter, look how beautiful your REGEX is !! 🙂
Copy link to clipboard
Copied
Thank you, Jean-Claude. And what a lovely and instructive graphic! How did you do that?
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.
Copy link to clipboard
Copied
Very nice tools, thank you.
Copy link to clipboard
Copied
Thanks Jean-Claude, very helpful.