Scan the input string for expression matches. Each match will return the matching tokens, start location, and end location. May be called with optional ``maxMatches`` argument, to clip scanning after 'n' matches are found. If ``overlap`` is specified, then overlap
( self, instring, maxMatches=_MAX_INT, overlap=False )
| 1816 | return tokens |
| 1817 | |
| 1818 | def scanString( self, instring, maxMatches=_MAX_INT, overlap=False ): |
| 1819 | """ |
| 1820 | Scan the input string for expression matches. Each match will return the |
| 1821 | matching tokens, start location, and end location. May be called with optional |
| 1822 | ``maxMatches`` argument, to clip scanning after 'n' matches are found. If |
| 1823 | ``overlap`` is specified, then overlapping matches will be reported. |
| 1824 | |
| 1825 | Note that the start and end locations are reported relative to the string |
| 1826 | being parsed. See :class:`parseString` for more information on parsing |
| 1827 | strings with embedded tabs. |
| 1828 | |
| 1829 | Example:: |
| 1830 | |
| 1831 | source = "sldjf123lsdjjkf345sldkjf879lkjsfd987" |
| 1832 | print(source) |
| 1833 | for tokens,start,end in Word(alphas).scanString(source): |
| 1834 | print(' '*start + '^'*(end-start)) |
| 1835 | print(' '*start + tokens[0]) |
| 1836 | |
| 1837 | prints:: |
| 1838 | |
| 1839 | sldjf123lsdjjkf345sldkjf879lkjsfd987 |
| 1840 | ^^^^^ |
| 1841 | sldjf |
| 1842 | ^^^^^^^ |
| 1843 | lsdjjkf |
| 1844 | ^^^^^^ |
| 1845 | sldkjf |
| 1846 | ^^^^^^ |
| 1847 | lkjsfd |
| 1848 | """ |
| 1849 | if not self.streamlined: |
| 1850 | self.streamline() |
| 1851 | for e in self.ignoreExprs: |
| 1852 | e.streamline() |
| 1853 | |
| 1854 | if not self.keepTabs: |
| 1855 | instring = _ustr(instring).expandtabs() |
| 1856 | instrlen = len(instring) |
| 1857 | loc = 0 |
| 1858 | preparseFn = self.preParse |
| 1859 | parseFn = self._parse |
| 1860 | ParserElement.resetCache() |
| 1861 | matches = 0 |
| 1862 | try: |
| 1863 | while loc <= instrlen and matches < maxMatches: |
| 1864 | try: |
| 1865 | preloc = preparseFn( instring, loc ) |
| 1866 | nextLoc,tokens = parseFn( instring, preloc, callPreParse=False ) |
| 1867 | except ParseException: |
| 1868 | loc = preloc+1 |
| 1869 | else: |
| 1870 | if nextLoc > loc: |
| 1871 | matches += 1 |
| 1872 | yield tokens, preloc, nextLoc |
| 1873 | if overlap: |
| 1874 | nextloc = preparseFn( instring, loc ) |
| 1875 | if nextloc > loc: |
no test coverage detected