1

I'm trying to run the following in my C#/IronPython:

import re
Message = re.sub(r"^EVN\|A\d+", "EVN|A08", Message, flags=MULTILINE)

This works fine on real python at the command prompt. However, once I put it into IronPython I get an error:

IronPython.Runtime.PythonContext.InvokeUnaryOperator(CodeContext context, UnaryOperators oper, Object target, String errorMsg)    
at IronPython.Runtime.Operations.PythonOps.Length(Object o)    
at IronPython.Modules.Builtin.len(Object o)    
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)    
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)    
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)    
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)    
at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)    
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)    
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)    
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)    
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)    
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)    
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)    
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)    
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)    
at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope)

Research led me to understand (right or wrong?) that the MULTILINE flag triggers Compile() in IronPython. Then I found this article about its lack of support in IronPython: https://ironpython.codeplex.com/workitem/22692.

Removing flags=MULTILINE fixes the error. However, It doesn't match on "^EVN" any longer.

EDIT: If I use flags=re.MULTILINE I receive this error:

ERROR Error while processing the message. Message: sub() got an unexpected keyword argument 'flags'. Microsoft.Scripting.ArgumentTypeException: sub() got an unexpected keyword argument 'flags'

END EDIT

My question is: How can I work around this issue and still get the same results I would get at the command line given the above code snippet, but in IronPython?

I rarely use Python, let alone IronPython, so please be forgiving as I'm not sure of my alternatives.

John S.
  • 1,937
  • 2
  • 18
  • 28
  • One thing you could do is replace newlines with some other character before applying the regular expression, so you no longer need the multi-line support. – kindall Apr 21 '15 at 20:38
  • With that import of `re`, it should be `re.MULTILINE`, not just `MULTILINE`. Is there a reason why you need to do this in IronPython if you’re calling it from C#, or is that not all of your Python code? – poke May 03 '15 at 16:43
  • @poke re.MULTILINE did not help. It is erroring on the "flags" saying the above error (I edited the comment with it just now) – John S. May 20 '15 at 21:04
  • 1
    It’s possible that IronPython doesn’t support flags that way. You can try compiling your expression first: `regex = re.compile('^EVN\|A\d+', re.MULTILINE)` and then do `Message = regex.sub('EVN|A08', Message)`. – poke May 20 '15 at 21:31

1 Answers1

1

It’s possible that IronPython does not support the flags keyword argument in re.sub. To work around that issue, you can compile your regular expression first. This is recommended anyway if you plan to use your expression multiple times; and the module-level functions do it anyway.

To do that, use re.compile. The flags can be passed as the second argument:

regex = re.compile('^EVN\|A\d+', re.MULTILINE)

This gives you a regular expression object, and you can directly use its sub method to perform your replacement:

Message = regex.sub('EVN|A08', Message)
poke
  • 369,085
  • 72
  • 557
  • 602