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
>>>
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