| |||||||||||
| |||||||||||
negative lookahead - example and alterative
Advanced Regular Expression Components example from a Well House Consultants training course
More on Advanced Regular Expression Components [link]
Source code: ex3.php Module: Q805
<?php
# negative lookahead - example and alterative # Solution looking for problem? - Look ahead foreach (file("railstats.xyz") as $line) { # - Look for bunch of non-tab characters followed by "Parkway" # - Is there the word "NULL" later on the line? If so, match fails # - If there is NOT a NULL, then match a tab followed by a number if (preg_match('/\t([^\t]+Parkway)(?!.*NULL)\t(\d+)/',$line,$gotten)) { print ($line); print_r ($gotten); } } # Easier way (sorry!) foreach (file("railstats.xyz") as $line) { if (preg_match('/Parkway.*NULL/',$line)) continue; if (preg_match('/\t([^\t]+Parkway)\t(\d+)/',$line,$gotten)) { print ($line); print_r ($gotten); } } /* Sample output ------------------------------------------------------- munchkin:re grahamellis$ php ex3.php 2598 LOT BL6 4JP SD 673086 53.5729974812 -2.4937754345 Lostock Parkway 118752 125475 126934 140033 208136 209168 Array ( [0] => Lostock Parkway 118752 [1] => Lostock Parkway [2] => 118752 ) 8720 HWI BL6 6LB SD 643092 53.5781205232 -2.5396742605 Horwich Parkway 217310 238184 262974 303858 408044 462000 Array ( [0] => Horwich Parkway 217310 [1] => Horwich Parkway [2] => 217310 ) 3230 BPW BS34 8PU ST 623796 51.5138357449 -2.5426595271 Bristol Parkway 1456350 1633280 1789848 1897941 2084200 2041548 Array ( [0] => Bristol Parkway 1456350 [1] => Bristol Parkway [2] => 1456350 ) 8530 WRP CV35 8RH SP 265654 52.2861149731 -1.6120622291 Warwick Parkway 352146 323773 386629 438722 494636 460556 Array ( [0] => Warwick Parkway 352146 [1] => Warwick Parkway [2] => 352146 ) 3474 TVP EX15 2QQ ST 044141 50.917528603 -3.3599570263 Tiverton Parkway 202588 232386 269760 303982 343960 354648 Array ( [0] => Tiverton Parkway 202588 [1] => Tiverton Parkway [2] => 202588 ) 3053 HDM HP17 8EP SP 730086 51.7708598641 -0.9421399671 Haddenham & Thame Parkway 432636 405151 468122 531365 557574 559770 Array ( [0] => Haddenham & Thame Parkway 432636 [1] => Haddenham & Thame Parkway [2] => 432636 ) 3645 LTN LU1 3JW TL 104206 51.8728619558 -0.3962015478 Luton Airport Parkway 1886911 2156699 2364740 2628187 2589262 2338658 Array ( [0] => Luton Airport Parkway 1886911 [1] => Luton Airport Parkway [2] => 1886911 ) 1861 SPK NG17 5LG SK 505577 53.1142086522 -1.2456451842 Sutton Parkway 126199 126055 119401 117685 112436 135732 Array ( [0] => Sutton Parkway 126199 [1] => Sutton Parkway [2] => 126199 ) 3030 DID OX11 7NR SU 524906 51.610955041 -1.2428912961 Didcot Parkway 2181924 2175689 2290949 2416635 2456384 2524260 Array ( [0] => Didcot Parkway 2181924 [1] => Didcot Parkway [2] => 2181924 ) 3501 BOD PL30 4BB SX 110641 50.4458558778 -4.6629711067 Bodmin Parkway 158172 166743 185498 203061 225140 221616 Array ( [0] => Bodmin Parkway 158172 [1] => Bodmin Parkway [2] => 158172 ) 4181 PTA SA13 1UR SS 766897 51.5917147337 -3.7813401092 Port Talbot Parkway 289640 300784 351561 403226 443626 450300 Array ( [0] => Port Talbot Parkway 289640 [1] => Port Talbot Parkway [2] => 289640 ) 1010 TAB WS10 0LD SP 016951 52.5529436752 -1.9762099948 Tame Bridge Parkway 142707 150722 177832 189670 364272 372092 Array ( [0] => Tame Bridge Parkway 142707 [1] => Tame Bridge Parkway [2] => 142707 ) 2598 LOT BL6 4JP SD 673086 53.5729974812 -2.4937754345 Lostock Parkway 118752 125475 126934 140033 208136 209168 Array ( [0] => Lostock Parkway 118752 [1] => Lostock Parkway [2] => 118752 ) 8720 HWI BL6 6LB SD 643092 53.5781205232 -2.5396742605 Horwich Parkway 217310 238184 262974 303858 408044 462000 Array ( [0] => Horwich Parkway 217310 [1] => Horwich Parkway [2] => 217310 ) 3230 BPW BS34 8PU ST 623796 51.5138357449 -2.5426595271 Bristol Parkway 1456350 1633280 1789848 1897941 2084200 2041548 Array ( [0] => Bristol Parkway 1456350 [1] => Bristol Parkway [2] => 1456350 ) 8530 WRP CV35 8RH SP 265654 52.2861149731 -1.6120622291 Warwick Parkway 352146 323773 386629 438722 494636 460556 Array ( [0] => Warwick Parkway 352146 [1] => Warwick Parkway [2] => 352146 ) 3474 TVP EX15 2QQ ST 044141 50.917528603 -3.3599570263 Tiverton Parkway 202588 232386 269760 303982 343960 354648 Array ( [0] => Tiverton Parkway 202588 [1] => Tiverton Parkway [2] => 202588 ) 3053 HDM HP17 8EP SP 730086 51.7708598641 -0.9421399671 Haddenham & Thame Parkway 432636 405151 468122 531365 557574 559770 Array ( [0] => Haddenham & Thame Parkway 432636 [1] => Haddenham & Thame Parkway [2] => 432636 ) 3645 LTN LU1 3JW TL 104206 51.8728619558 -0.3962015478 Luton Airport Parkway 1886911 2156699 2364740 2628187 2589262 2338658 Array ( [0] => Luton Airport Parkway 1886911 [1] => Luton Airport Parkway [2] => 1886911 ) 1861 SPK NG17 5LG SK 505577 53.1142086522 -1.2456451842 Sutton Parkway 126199 126055 119401 117685 112436 135732 Array ( [0] => Sutton Parkway 126199 [1] => Sutton Parkway [2] => 126199 ) 3030 DID OX11 7NR SU 524906 51.610955041 -1.2428912961 Didcot Parkway 2181924 2175689 2290949 2416635 2456384 2524260 Array ( [0] => Didcot Parkway 2181924 [1] => Didcot Parkway [2] => 2181924 ) 3501 BOD PL30 4BB SX 110641 50.4458558778 -4.6629711067 Bodmin Parkway 158172 166743 185498 203061 225140 221616 Array ( [0] => Bodmin Parkway 158172 [1] => Bodmin Parkway [2] => 158172 ) 4181 PTA SA13 1UR SS 766897 51.5917147337 -3.7813401092 Port Talbot Parkway 289640 300784 351561 403226 443626 450300 Array ( [0] => Port Talbot Parkway 289640 [1] => Port Talbot Parkway [2] => 289640 ) 1010 TAB WS10 0LD SP 016951 52.5529436752 -1.9762099948 Tame Bridge Parkway 142707 150722 177832 189670 364272 372092 Array ( [0] => Tame Bridge Parkway 142707 [1] => Tame Bridge Parkway [2] => 142707 ) ---------------------------------------------------- */ ?> Learn about this subject
This module and example are covered on our public Regular Expressions course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or on site course for you.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering all the subjects we teach are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Advanced Regular Expression Components" training module. You'll find a description of the topic and some
other closely related examples on the "Advanced Regular Expression Components" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |