Regex lookbehind

3

I need to write a regex to parse a snippet of a swift message field, so I'm using loockbehind.

In the example below I have the two strings that I need from the 12 bic positions. In the first string would be TESTBIC34XXX In the second string YOURBANKXJKL

The problem is that the fields between O564 and TESTBIC34XXX are optional

String1 = "2:O5641057130214TESTBIC34XXX26264938281302141757N"
String2 = "2:I101YOURBANKXJKLU3003"

I tried to use lookbehind by varying the digits from 3 to 13 because the 3 digits after the "I" or "O" are required. Not resolved because the regex will always find the first 3 digits and will stop.

Pattern = (?<=[IO]\d{3,13})\w{12}

I have tried several conditional approaches without success. Anyone have any suggestions?

    
asked by anonymous 09.03.2017 / 22:34

2 answers

1

% s of javascript% s do not support lookbehind . Is there any reason you can not say Regex ? After all, a regex with lookbehind is semantically equivalent to one with a catch group prefixed with lookbehind ...

EDIT: .NET has lookbehind , but apparently it is not greedy >. You can, in the latter case, use the same javascript pattern above and say /[IO]\d{3,13}(\w{12})/.exec(String1)[1] and Regex.Match(@"[IO]\d{3,13}(\w{12})", String1).Groups[1].Value to get the code you want in both examples.

    
09.03.2017 / 22:56
2

Rules (as I understand it)

  • Must start with I or O.
  • [IO] must be followed by at least 3 digits.
  • After the digits and if you start text you should capture the next 12 characters.

Standard REGEX

[IO]\d{3,}(\w{12}) See in REGEX101

Problem

You do not want to capture what comes before, so you're using loockbehind loockbehind . His problem is that he does not accept quantifiers , that is he wants you to be specific in relation to what comes before.

Then your problem is there, you could not use loockbehind , since you yourself said:

  

The problem is that the fields between O564 and TESTBIC34XXX are optional

loockbehind would be to ensure that a particular sentence occurs before capture, to ensure specificity a>.

Resolution

You may even be able to mount a REGEX to capture just what you want, but instead of wasting all that time, I suggest simply keeping REGEX default and working with the capture group. Using Match[1] .

Note

  • All Problem links show why not use loockbehind (in your case).
10.03.2017 / 13:21