Regex fun

Here is a session of testing regex. The goal is to find how to detect a java import statement at the beginning of a line.

For example;

Detect this line:

"import java.blah.Com;"

>>> print re.match("^(import).$", a)
None
>>> print re.match("^import.$", a)
None
>>> print re.match("^import$", a)
None
>>>
>>> a
'import lkjksdjlfksjldkfjsd '
>>> print re.match("^[import]$", a)
None
>>> print re.match("^[import]$", a)
None
>>> a = "import"
>>> print re.match("^[import]$", a)
None
>>> print re.match("[import]", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import" ; print a
import
>>> a = "import" ; print re.match("[import]", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import " ; print re.match("[import]", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "import " ; print re.match("^[import]", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = " import " ; print re.match("^[import]", a)
None
>>> a = " import " ; print re.match("^[import]$", a)
None
>>> a = "import " ; print re.match("^[import]$", a)
None
>>> a = "import" ; print re.match("^[import]$", a)
None
>>> a = "import" ; print re.match("^[import].", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "import " ; print re.match("^[import].", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = "import " ; print re.match("^[import].", a).end()
2
>>> a = "import " ; print re.match("^[import].", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import " ; print re.match("^[import].", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "import " ; print re.match("^[import]", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = "import " ; print re.match("^[import].\Z", a)
None
>>> a = "import " ; print re.match("^[import].\$", a)
None
>>> a = "import " ; print re.match("^[import].\Z", a)
None
>>> a = "import" ; print re.match("^[import].\Z", a)
None
>>> a = "import" ; print re.match("[import].\Z", a)
None
>>> a = "import " ; print re.match("[import].\Z", a)
None
>>> a = "import " ; print re.match("^[import].*\Z", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import " ; print re.match("^[import].*$", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "import" ; print re.match("^[import].*$", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = "impor" ; print re.match("^[import].*$", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import " ; print re.match("^[import].*$", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "ort " ; print re.match("^[import].*$", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = "ort " ; print re.match("^import.*$", a)
None
>>> a = "import " ; print re.match("^import.*$", a)
<_sre.SRE_Match object at 0x7ff35678>
>>> a = "ximport " ; print re.match("^import.*$", a)
None
>>> a = "import " ; print re.match("^import.*$", a)
<_sre.SRE_Match object at 0x7ff35790>
>>> a = "import " ; print re.match("^import.*\;$", a)
None
>>> a = "import ;" ; print re.match("^import.*\;$", a)
<_sre.SRE_Match object at 0x7ff35758>
>>> a = "import " ; print re.match("^import.*\;$", a)
None
>>>

Comments

Popular posts from this blog

On Unit Testing, Java TDD for developers to write

Is Java the new COBOL? Yes. What does that mean, exactly? (Part 1)

JVM Notebook: Basic Clojure, Java and JVM Language performance