diff --git a/src/xercesc/util/regx/RegularExpression.cpp b/src/xercesc/util/regx/RegularExpression.cpp
index d8927a53de69c95906de155f150ad5eb0f923187..1436f9f9d424aa04a49c8050e79256cae0c0e8db 100644
--- a/src/xercesc/util/regx/RegularExpression.cpp
+++ b/src/xercesc/util/regx/RegularExpression.cpp
@@ -135,6 +135,62 @@ RegularExpression::Context::Context(MemoryManager* const manager) :
 {
 }
 
+RegularExpression::Context::Context(Context* src) :
+	fAdoptMatch(false)
+    , fStart(src->fStart)
+	, fLimit(src->fLimit)
+	, fLength(src->fLength)
+	, fSize(src->fSize)
+    , fStringMaxLen(src->fStringMaxLen)
+	, fOffsets(0)
+	, fMatch(0)
+	, fString(src->fString)
+    , fMemoryManager(src->fMemoryManager)
+{
+	if(src->fOffsets)
+    {
+		fOffsets = (int*) fMemoryManager->allocate(fSize* sizeof(int));
+	    for (int i = 0; i< fSize; i++)
+		    fOffsets[i] = src->fOffsets[i];
+    }
+    if(src->fMatch)
+    {
+        fMatch=new Match(*src->fMatch);
+        fAdoptMatch=true;
+    }
+}
+
+RegularExpression::Context& RegularExpression::Context::operator= (const RegularExpression::Context& other)
+{
+    fStart=other.fStart;
+	fLimit=other.fLimit;
+	fLength=other.fLength;
+	fSize=other.fSize;
+    fStringMaxLen=other.fStringMaxLen;
+	fString=other.fString;
+    if (fOffsets)
+        fMemoryManager->deallocate(fOffsets);//delete [] fOffsets;
+    fOffsets=0;
+	if (fAdoptMatch)
+		delete fMatch;
+    fMatch=0;
+	fAdoptMatch=false;
+
+    fMemoryManager=other.fMemoryManager;
+	if(other.fOffsets)
+    {
+		fOffsets = (int*) fMemoryManager->allocate(fSize* sizeof(int));
+	    for (int i = 0; i< fSize; i++)
+		    fOffsets[i] = other.fOffsets[i];
+    }
+    if(other.fMatch)
+    {
+        fMatch=new Match(*other.fMatch);
+        fAdoptMatch=true;
+    }
+    return *this;
+}
+
 RegularExpression::Context::~Context()
 {
     if (fOffsets)
@@ -1330,6 +1386,29 @@ int RegularExpression::matchCapture(Context* const context, const Op* const op,
 	return ret;
 }
 
+int RegularExpression::matchUnion(Context* const context,
+                                   const Op* const op, int offset,
+                                   const short direction)
+{
+  unsigned int opSize = op->getSize();
+
+  Context bestResultContext;
+  int bestResult=-1;
+  for(unsigned int i=0; i < opSize; i++) {
+      Context tmpContext(context);
+      int ret = match(&tmpContext, op->elementAt(i), offset, direction);
+      if (ret >= 0 && ret <= context->fLimit && ret>bestResult)
+      {
+          bestResult=ret;
+          bestResultContext=tmpContext;
+      }
+  }
+  if(bestResult!=-1)
+      *context=bestResultContext;
+  return bestResult;
+}
+
+
 bool RegularExpression::matchCondition(Context* const context,
                                               const Op* const op, int offset,
                                               const short direction)
diff --git a/src/xercesc/util/regx/RegularExpression.hpp b/src/xercesc/util/regx/RegularExpression.hpp
index 29e73aa00835e3c484450ed7b44300654c3af22e..c388d8a40b7eee28d5a998117df66b891788655f 100644
--- a/src/xercesc/util/regx/RegularExpression.hpp
+++ b/src/xercesc/util/regx/RegularExpression.hpp
@@ -158,8 +158,10 @@ private:
     {
         public :
             Context(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
+            Context(Context* src);
             ~Context();
 
+            Context& operator= (const Context& other);
             inline const XMLCh* getString() const { return fString; }
             void reset(const XMLCh* const string, const int stringLen,
                        const int start, const int limit, const int noClosures);
@@ -548,24 +550,6 @@ private:
       return ret;
   }
 
-  inline int RegularExpression::matchUnion(Context* const context,
-                                           const Op* const op, int offset,
-                                           const short direction)
-  {
-      unsigned int opSize = op->getSize();
-      int ret = -1;
-
-      for (unsigned int i=0; i < opSize; i++) {
-
-          ret = match(context, op->elementAt(i), offset, direction);
-
-          if (ret >= 0 && ret <= context->fLimit)
-              return ret;
-      }
-
-      return -1;
-  }
-
   inline int RegularExpression::matchModifier(Context* const context,
                                               const Op* const op, int offset,
                                               const short direction)