How to workaround IronPython Compile() Issue?
Question: 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.


Answer: It’s possible that IronPython does not support the `flags` keyword argument in
[`re.sub`](https://docs.python.org/2/library/re.html#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`](https://docs.python.org/2/library/re.html#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`](https://docs.python.org/2/library/re.html#re.RegexObject.sub) method
to perform your replacement:

    
    
    Message = regex.sub('EVN|A08', Message)
    

