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

Avoid displaying passwords in source code

Currently, I am working on developing a password manager web application similar to LastPass. One issue that has come to my attention is the visibility of variables containing decrypted passwords in the source code after retrieving them from a database u ...

Adding jQuery and other libraries to Typescript for optimal functionality

After spending days researching and struggling, I am reaching out here for clarification on the process of importing a library in Typescript. I used to just add the script tag and everything would work fine. Now that I am working on building a MEAN-Stack ...

The res.download() function is not functioning properly when called from within a function, yet it works perfectly when directly called through the API in a browser

I have a button that, when clicked, triggers the downloadFile function which contacts the backend to download a file. async downloadFile(name) { await this.$axios.$get(process.env.API_LINK + '/api/files/' + name) }, app.get('/api/files/ ...

reveal a hidden div by sliding it during an onclick action

My PHP while loop code is as follows: while (...($...)){ $convid = $row['ID']; echo" <button onclick='getconvo($convid)'>open</button> <div class="convowrap"></div> "; } Here is the correspond ...

Validating HTML using EJS templates set as "text/template" elements

What is the general consensus on HTML validation when utilizing a framework such as Backbone or Meteor and generating views in the client from EJS templates? An issue arises with the fact that name is not considered an official attribute for a <script& ...

Exploring Parquet Files with Node.js

Looking for a solution to read parquet files using NodeJS. Anyone have any suggestions? I attempted to use node-parquet but found it difficult to install and it struggled with reading numerical data types. I also explored parquetjs, however, it can only ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

Validation of New Relic License Key

Is there a way to verify the validity of a provided New Relic license key in a JavaScript application? I have searched through the documentation but did not come across any API endpoint for this purpose. UPDATE: Just to clarify, we do not have access to ...

Error: Unable to access undefined properties (reading 'url')

I am currently working on creating a drag-and-drop card game and I have encountered an issue with the react-dnd library. When using data from the file, everything works fine, but if I have to fetch the data externally, it throws an error. This problem see ...

Error: The "toString" property of an undefined variable cannot be read in uploadify

I'm having trouble with uploadify and trying to debug the issue. When attempting to use uploadify in Chrome, I encounter the following error: Uncaught TypeError: Cannot read property 'toString' of undefined Below is my html code: <li ...

Tips for moving and filling data in a different component using NextJS

Currently, I am developing an application using Next.js and tailwindcss. The Issue In essence, I have a table consisting of 4 columns where each row contains data in 3 columns and the last column includes an "Update" button. The data in each row is genera ...

The placement of the Vuetify tooltip is incorrectly aligned when located in the footer section

Having trouble fixing the issue with the Vuetify tooltip. After scrolling on the page, the tooltip moves up despite using fixed="true". Here is the code snippet causing the problem: <v-footer app inset fixed> <v-row align="center ...

Slider Volume with jQuery

Struggling to find a solution for this issue. Seeking some assistance. My goal is to create a basic volume slider. So, the orange section represents my volume slider. This is the jQuery code I am using: var mouseIsDown = false; $("#volSlider").on("mou ...

A proposal for implementing constructor parameter properties in ECMAScript

TypeScript provides a convenient syntax for constructor parameter properties, allowing you to write code like this: constructor(a, public b, private _c) {} This is essentially shorthand for the following code: constructor(a, b, _c) { this.b = b; thi ...

Removing all Null Form Inputs from the Document Object Model upon Submission utilizing JavaScript

I am currently working on a conditional Shopify form that was passed down to me from another developer. The form utilizes JavaScript/Jquery for field validation, ensuring that all mandatory fields are completed before proceeding to the next step. After mak ...

Tips for integrating the react-financial-charts library into your React and JavaScript project

While exploring the react-financial-charts library, I discovered that it is written in TypeScript (TS). Despite my lack of expertise in TypeScript, I am interested in using this library in my React+JS project due to its active contributions. However, I hav ...

Implementing custom CSS styles to a component in real-time with ng-style

When I use an angularjs component to create stylized pictures, the style is not applied correctly on the first visit to a page (refreshing the page shows the correct style). The CSS that should be applied is generated dynamically based on the height and wi ...

Tips for deactivating all JavaScript events on a webpage that has been loaded within an iframe

My website is equipped with various JS click/mouseover/mouseout/scroll events and incorporates several JS libraries to operate features such as carousels, bootstrap modal pop-ups, and more. I am looking to halt all events on a specific element. Although I ...

Verify the validation of the text box

Checking a textbox control for validation is necessary. Validation Criteria: Value should be between 0 and 1000, with up to 2 decimal places (e.g. 1.00, 85.23, 1000.00). Once 2 decimal points are used, users should not be able to enter additional ze ...

AngularJS application is throwing an error indicating provider $q is not recognized

Could someone please advise on what might be the issue with my code snippet below: var app = angular.module('app', [ 'angular-cache', 'angular-loading-bar', 'ngAnimate', 'ngCookies', &a ...