1

I am trying to get last 3 characters of a pattern. But I am stuck on how to do it.

Please share your thoughts on this.

PACKAGE uima.ruta.example;

Document{->RETAINTYPE(SPACE)};

DECLARE VarA;

((W|NUM)* (W|NUM)*){REGEXP(".{12}")-> MARK(VarA),MARK(EntityType,1), UNMARK(VarA)};

I/P - AB1234567CAB

O/P - CAB

The fourth bird
  • 154,723
  • 16
  • 55
  • 70

3 Answers3

1

You can use $ to indicate where the end of the source string should be in the pattern. For your example, you want the last 3 characters, so you could use a pattern like:

.{3}$

To get the last 3 characters. This would get any character (apart from a \n), but you could be more specific, for example if you just want uppercase letters, you could use:

[A-Z]{3}$

or if you could accept uppercase, lowercase or numbers, you could use

\w{3}$

Experiment on regex101.com to see what works for you.

  • Thanks for your input @Jamie, but I have a sentence: Example - I/p - **The policy number is AB1231123PAD. Requesting it to be done ASAP.** Exp O/P - **PAD** Your suggestion will work only if my pattern is at the end. But I want the **3 characters(PAD)** to be extracted from the format of **policy number (AB1231123PAD)** irrespective of wherever it is. – Vamsi Unique Jul 15 '19 at 06:33
1

Suppose your data in cell A1 You can Use the second macro of this two ones

Option Explicit

Sub Extract_Laste_3Carachters(st As Range, Patt$, n)
  Dim Obj As Object
  Set Obj = CreateObject("Vbscript.RegExp")
  With Obj
   .Pattern = Patt
   .Global = True
  End With
  If Len(st) <= 3 Then st.Offset(, 1) = st: Exit Sub
   If Obj.test(st) Then
        If n > Obj.Execute(st).Count Then n = Obj.Execute(st).Count
          st.Offset(, 1) = _
        Obj.Execute(st)(n - 3) _
        & Obj.Execute(st)(n - 2) _
        & Obj.Execute(st)(n - 1)
    End If
End Sub

'+++++++++++++++++++++++++++++++

Sub Test_Me()
Call Extract_Laste_3Carachters(Range("a1"), ("\w"), Len(Range("a1")))
End Sub
1

I tried below code and its working now!

PACKAGE uima.ruta.example;

Document{->RETAINTYPE(SPACE)};

"(?i)\\b(?=.*\\d)[1]{0,1}[A-Z0-9]{2}[\\s |-]{0,2}[A-Z0-9]{7}[\\s |-]{0,2}([A-Z]{3})\\b" ->1 = EntityType;
AJ.
  • 4,526
  • 5
  • 29
  • 41