How to use variables inside regular expressions in Jython ?

View: New views
3 Messages — Rating Filter:   Alert me  

How to use variables inside regular expressions in Jython ?

by Ranga Raju :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
 
I am trying to use variable in regular expressions but its not working for some reason.
 
I am using Jython that comes with WebSphere 6.1.
 
wsadmin>print oNodes
ndm-ws16a(cells/WS16/nodes/ndm-ws16a|node.xml#Node_1)
wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)
wsadmin>
 
wsadmin>print oNodeList
['ndm-ws16a(cells/WS16/nodes/ndm-ws16a|node.xml#Node_1)', 'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)']
wsadmin>
 
wsadmin>print nodeName
wpdiu528
wsadmin>
 
wsadmin>expr = r".*wpdiu528.*"
wsadmin>pat4 = re.compile(expr)
wsadmin>m = re.search(expr,oNodes)
wsadmin>print m
org.python.modules.sre.MatchObject@...
wsadmin>
wsadmin>m.group()
'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)'
 
This 'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)' is the desired result. I am able to achieve this result when I provide "wpdiu528" directly in variable expr. But I would like to use another variable called nodeName in place of the string "wpdiu528" inside the regular expression.
 
I tried as shown below, but it fails.

wsadmin>expr = r".*%nodeName.*"
wsadmin>pat4 = re.compile(expr)
wsadmin>m = re.search(expr,oNodes)
wsadmin>print m
None
wsadmin>
wsadmin>m.group()
WASX7015E: Exception running command: "m.group()"; exception information:
 com.ibm.bsf.BSFException: exception from Jython:
Traceback (innermost last):
  File "<input>", line 1, in ?
AttributeError: 'None' object has no attribute 'group'
wsadmin>
 
Please see if you can assist me here...
 

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: How to use variables inside regular expressions in Jython ?

by Anthony Smith-11 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Ranga,

On 5 Jun 2009, at 01:18, Ranga Raju C.V.S. wrote:

> This 'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|
> node.xml#Node_1)' is the desired result. I am able to achieve this  
> result when I provide "wpdiu528" directly in variable expr. But I  
> would like to use another variable called nodeName in place of the  
> string "wpdiu528" inside the regular expression.


Try
expr = ".*" + nodeName + ".*"

 >>> expr = ".*" + nodeName + ".*"
 >>> pat4 = re.compile(expr)
 >>> m = re.search(expr, oNodes)
 >>> m.group()
'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)'

Cheers,

Anthony

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users

Re: How to use variables inside regular expressions in Jython ?

by Wizzzard :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Anthony Smith schrieb:

> Hi Ranga,
>
> On 5 Jun 2009, at 01:18, Ranga Raju C.V.S. wrote:
>
>> This 'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|
>> node.xml#Node_1)' is the desired result. I am able to achieve this  
>> result when I provide "wpdiu528" directly in variable expr. But I  
>> would like to use another variable called nodeName in place of the  
>> string "wpdiu528" inside the regular expression.
>
>
> Try
> expr = ".*" + nodeName + ".*"
>
>  >>> expr = ".*" + nodeName + ".*"
>  >>> pat4 = re.compile(expr)
>  >>> m = re.search(expr, oNodes)
>  >>> m.group()
> 'wsejbfpp1wpdiu528(cells/WS16/nodes/wsejbfpp1wpdiu528|node.xml#Node_1)'
>

Why not this?

expr = ".*%s.*" % nodeName

Regards,
Roland.

------------------------------------------------------------------------------
OpenSolaris 2009.06 is a cutting edge operating system for enterprises
looking to deploy the next generation of Solaris that includes the latest
innovations from Sun and the OpenSource community. Download a copy and
enjoy capabilities such as Networking, Storage and Virtualization.
Go to: http://p.sf.net/sfu/opensolaris-get
_______________________________________________
Jython-users mailing list
Jython-users@...
https://lists.sourceforge.net/lists/listinfo/jython-users