Announcement

Collapse
No announcement yet.

RegEX Regular Expression & Formula string\match

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • RegEX Regular Expression & Formula string\match

    I'm playing with the "big" league now! Playing with regular expression!

    Anyway I'm not sure if this is possible with EspoCRM, anyone do something similar or have the knowledge?

    I got a street address like so:

    123 Main Street, Suburb City 2000

    Style as
    [Street number Street name and Street type], [City name] [Postal code]

    Using the regex I found online like so I can get all data in different group like so: (tested with https://regex101.com/)

    Regex Code
    Code:
    /\b(\d+\s+[\w\s]+)\s*,?\s*(\b(?:Road|Street|Avenue|Lane|Drive|Boulevard|Crescent|Court|Place|Terrace|Highway|Parade|Close|Grove|Way|Walk|Esplanade))\b(?:\s*,\s*)?(\b[A-Z][a-z]+\b(?:\s+[A-Z][a-z]+\b)*)?\s*(\b\d{4})\b/
    Test Data:
    Code:
    123 Main Street, Suburb City 2000
    As you can see it is working and it have separate the data into different group
    Click image for larger version  Name:	image.png Views:	0 Size:	19.0 KB ID:	93583

    Now what I want to do next using the formula is this:
    adddressStreet = (group1)
    addressCity = (group2)

    For example:
    addressStreet = '123 Main'

    Formula wise it will look something like this:

    Code:
    output\printLine(
    
    ifThen(formulaHelper!=null,
        addressStreet=string\match(formulaHelper, '/RegEX Group 1 here/');
        addressCity=string\match(formulaHelper, '/RegEX Group 3 here/')
    formulaHelper is just a custom field so I can paste data and it "extract" data for me.
    Of course the example above will not work! Because I don't know the correct code.

    And here I am, asking. Thank you.

    --- some more screenshot for reference
    Click image for larger version

Name:	image.png
Views:	77
Size:	96.5 KB
ID:	93584

    My failure:
    Click image for larger version

Name:	image.png
Views:	79
Size:	40.3 KB
ID:	93585

  • #2
    Using the match function, you can't retrieve groups 2, 3, etc. If you want to access a group other than the first one, you should create your own function. If I understood the question correctly, that is.
    Click image for larger version

Name:	image.png
Views:	74
Size:	52.2 KB
ID:	93594

    Comment


    • espcrm
      espcrm commented
      Editing a comment
      Thank you dimyy! I guess I need to look at that code and add $matches[1];

      Anyway this is a big clue and tips!
Working...
X