December 2023
Intermediate to advanced
242 pages
5h 28m
English
| Recipe 41 | Forcing ^ and $ to Match at the Start and End of a Line with the m Flag |
Imagine you have a database of movie subtitles and want to provide a search mechanism that returns complete lines containing a specific word. You have already seen how to assert the start and end of a string in Recipe 24, Asserting the Start or End of a String with ^ and $.
But this time, you want to assert the start and end of a line. So, if the search word is “McClaren” and the subtitle file contains these lines:
| | ... |
| | |
| | 491 |
| | 00:39:21,396 --> 00:39:23,481 |
| | Soon as you can, get that plane ready. |
| | |
| | 492 |
| | 00:39:23,565 --> 00:39:27,485 |
| | Dr. McClaren could lose his leg |
| | without proper medical attention. |
| | |
| | ... |
You want to retrieve only the line containing “McClaren”: ...
Read now
Unlock full access