Struggling to find multiline content in a SWIFT message using regex

Looking into a SWIFT message using RegEx, here is an excerpt:

:16R:FIN
:35B:ISIN CH0117044708
ANTEILE -DT USD- SWISSCANTO (CH)
INDEX EQUITY FUND USA
:16R:FIA

The goal is to extract information in group 3:

ISIN CH0117044708
ANTEILE -DT USD- SWISSCANTO (CH)
INDEX FUND V - SWISSCANTO (CH)
INDEX EQUITY FUND USA

However, only ISIN CH0117044708 is being captured.

Currently troubleshooting the RegEx issue with this expression:

/:([0-9]{2}[A-Z]){1}(::|:)((.*\r\n){1,4}|.*)/gm

To experiment further, check out this link: https://regex101.com/r/qX9cET/2

Edit: Exploring how to match this optional pattern:

([A-Z]*)(?:\/\/)?(.*(?:\/)?){0,2}

  • No // and / in line
  • // and a single /
  • // and two /

Incorporated in the previous example (https://regex101.com/r/Ubci69/5):

:16R:FIN
:97A::SAFE//0123-456789-11-020
:35B:ISIN CH0117044708
ANTEILE -DT USD- SWISSCANTO (CH)
INDEX FUND V - SWISSCANTO (CH)
INDEX EQUITY FUND USA
:16R:FIA
:93B::AGGR//UNIT/0,117
:19A::HOLD//CHF237,15
:92B::EXCH//JPY/CHF/0,0087535442107

Answer №1

To capture in the third group, consider using [\s\S] instead of the dot to include whitespace characters and a negative lookahead (?! to ensure that what follows does not match :[0-9]{2}[A-Z]:{1,2} which is the initial pattern being sought.

You can also remove {1} if unnecessary and skip the first two capturing groups to centralize your values in just the first group.

:([0-9]{2}[A-Z])(::|:)((?:[\s\S](?!:[0-9]{2}[A-Z]:))*)

Check out this Regex Demo

Explanation

  • :: Matches literally
  • ([0-9]{2}[A-Z]): Captures 2 digits followed by an uppercase character in the first group
  • (::|:): Captures two or one colon(s) in the second group
  • (: Starts the third group
    • (?:: Non-capturing group
      • [\s\S]: Matches any character, including whitespaces
      • (?!:: Negative lookahead to ensure it doesn't match
        • [0-9]{2}[A-Z]:: Captures 2 digits followed by an uppercase letter and a colon in the first group
      • ): Closes the negative lookahead
    • )*: Closes non-capturing group and repeats zero or more times
  • ): Closes the third group

Update: A more optimized version of the above regex utilizing the dot. This will match the specified pattern with colons at the beginning, then proceed to match any characters until the end of the line with the option of encountering a line break. It uses a negative lookahead to ensure exclusion of the colon part, repeating the whole line in a continuous pattern.

:([0-9]{2}[A-Z])(::|:)(.*(?:\r?\n)?(?:(?!:[0-9]{2}[A-Z]:).*(?:\r?\n)?)*)

See this Regex demo for reference

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Make sure that JSON.stringify is set to automatically encode the forward slash character as `/`

In my current project, I am developing a service using nodejs to replace an old system written in .NET. This new service exposes a JSON API, and one of the API calls returns a date. In the Microsoft date format for JSON, the timestamp is represented as 159 ...

What is the most effective method for verifying a selected item in Jquery UI selectable?

I'm having an issue with my image display div where users can delete selected images. The code functions correctly, but there seems to be unnecessary repetition in certain parts of it. I attempted using `$(".ui-selected").each()` to stop the ...

Struggling to determine whether an array contains data or is void in ReactJS?

In the state, I have an array and I set the default value of my state to an empty array []. After loading an API request, I need to display a loader until the data is ready. So, I am using a condition like this: (if the array length === 0, the loader wil ...

Update the Material UI input field value using an external JavaScript script

Imagine I find myself exploring an online form that utilizes Material UI components, for instance, this example link. I am interested in automatically filling the input fields with a specific value using JavaScript in the console: for (input of document.g ...

New navigation menu changes as user scrolls

I'm struggling to find a way to implement a different navigation menu that appears when the user scrolls down. I'm envisioning something similar to this example: My goal is to have #small-menu replace #big-menu once the user starts scrolling. C ...

When a new entry is added to the database, automatically refresh a <div> section within an HTML document

I have a basic webpage that showcases various products stored in the database. My goal is to implement an updater feature where, if a user adds a new product, the page will automatically display the latest addition in a specific div. I attempted to refere ...

v-for loop to populate dropdown menu with identical values

Recently, I started working on a points counter app using Vue.js and encountered some issues with the v-for loop and dropdown menu functionality. Within my Vue.js application, I have an array of players as shown below: var app = new Vue({ el: '#l ...

"Encountering an issue with Multer where req.file is displaying as undefined in NodeJS

Recently, I followed the advice of several YouTubers and used multer for file upload in my project. However, despite correctly defining all the functions, req.file always appears as undefined. booking_route.js const express = require('express'); ...

What is the best way to utilize bilinear color interpolation with JavaScript?

I'm grappling with the concept of bilinear interpolation, wondering if there's a more efficient method than what I've attempted below using the Culori library's interpolate() function. My uncertainty lies in whether it's correct t ...

Issue with Laravel: Using `$request->all()` results in an empty array when called using JSON XHR

Having trouble using $.ajax and only the XMLHttpRequest for sending JSON to a Laravel controller. Keep getting 500 errors when attempting to make the request. Here's the method I'm using to send the data: const sendEdit = function(){ ...

Guide on how to have two controllers execute identical tasks in Angular while modifying the appearance of the website

Trying to recreate Google's homepage functionality using Angular has been challenging for me. Despite watching Egghead videos and studying the API extensively, I couldn't find a specific example for this behavior. Here's what I aim to achiev ...

What is the best way to retrieve a {collection object} from a JavaScript map?

My application utilizes a third-party library that returns the map in the following format: public sids: Map<SocketId, Set<Room>> = new Map(); When I try to access it using the code below: io.of("/").adapter.sids.forEach(function(va ...

Fixing the problem of digest overflow in AngularJS

I've been working on a code to display a random number in my view, but I keep encountering the following error message: Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! It seems to be related to a digest outflow issue, and I&apo ...

Iterating over a range of values with _.each()

Can someone help me figure out why my syntax is incorrect for applying two values from different iteratees (day.classes and event.part) on line 5? <div class="days"> <div class="headers"> <% _.each(daysOfTheWeek, function(day) { %&g ...

Looking to enable input customization in a rendered ReactJS component? Should I elevate the state?

On my webpage, I successfully displayed the values Hello and World, but currently, they cannot be edited. Even though the text cursor shows up when clicked on, keyboard input doesn't register. How can I enable input functionality like deleting or addi ...

injecting a variable from the configuration service into a TypeScript decorator

I am interested in setting up a scheduled task for my NestJs application to run at regular intervals. I found information on how to use intervals in the NestJs documentation. Since my application uses configuration files, I want to keep the interval value ...

Passing an array of ID's between two components in Angular: A comprehensive guide

Greetings fellow readers, I have encountered a new challenge in my Angular project. I need to pass an array of IDs from one component to a completely unrelated component. Most solutions suggest using ViewChild, Input, or Output, but since the components ar ...

Overlay a small image on top of a larger background image using canvas, then merge them into a single set of pixel

Is there a way to combine a smaller image with a larger background image on one canvas while allowing the smaller image to move around? I'll need to save the original background pixel data so that each frame can be redrawn with the overlay in its upda ...

Encountering difficulties triggering the click event in a JavaScript file

Here is the example of HTML code: <input type="button" id="abc" name="TechSupport_PartsOrder" value="Open Editor" /> This is the jQuery Code: $('#abc').click(function () { alert('x'); }); But when I move this jQuery code to a ...

Troubaling with AngularJS Routing issues

I am encountering an issue with my routing system. The "otherwise" case is functioning correctly, however, when I click on a menu item, the routing does not load the corresponding page automatically. Can someone assist me in identifying what is wrong with ...