Regex lookbehind

Asked

Viewed 102 times

3

I need to write a regex to parse an excerpt from a field of the Swift message, so I’m using loockbehind.

In the example below I have the two strings I need from the 12 positions of Bic. In the first string it 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 using lookbehind by varying the digits from 3 to 13, because the 3 digits after "I" or "O" are required. It didn’t because regex will always find the first 3 digits and will stop.

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

I’ve tried several unsuccessful conditional approaches. Does anyone have any suggestions ?

2 answers

2

Rules (as I understood)

  • It should 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.

REGEX Standard

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

Problem

You don’t want to capture what comes before, so you’re using the loockbehind. His problem is that it does not accept quantifiers, that is to say he wants you to be specific in relation to what comes before.

So your problem is there, you couldn’t use the loockbehind, since you said so yourself:

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

The loockbehind would be to ensure that a given sentence occurs before the capture, to ensure a specificity.

Resolution

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

Note

  • All links from Problem, show why not use loockbehind (in your case).
  • Guilherme, thanks for the full answer, helped a lot to clarify the concept of the lookbehind, very good reference, I ended up marking Wtrmute’s response because he answered before. But anyway thanks for the help.

  • @user1987610 Quiet, good that helped :D

1


Regexjavascript s do not have support for lookbehind. Is there any reason why you can’t say /[IO]\d{3,13}(\w{12})/.exec(String1)[1]? After all, a regex with lookbehind is semantically equivalent to one with a capture group prefixed with the lookbehind...

EDIT: . NET has lookbehind, but apparently he’s not greedy. You can ultimately use the same javascript pattern as above and say Regex.Match(@"[IO]\d{3,13}(\w{12})", String1).Groups[1].Value and Regex.Match(@"[IO]\d{3,13}(\w{12})", String2).Groups[1].Value to get the code you want in both examples.

  • Opa ok. I’m not doing the match by JS. It’s Java and .Net. I’m using the lookbehind to not receive the prefix, I just really want the w{12}

  • Got it. good. Thanks for the answer, solved the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.