Chrysanthi
Chrysanthi

Reputation: 11

RegEx in SAP 7.5 start with and end after first occurrence of character

I am working on SAP NetWeaver 7.5 and having the following string

/InstrId/**BTXXXXXXXXX**/ /EndToEndId/REF 2102231XX4/ /BICFI/XXXXXRAAXXX/

I need to get as a result BTXXXXXXXXX which should also be not more than 16 characters.

I have tried ^/InstrId/(.*)/$

But I get BTXXXXXXXXX/ /EndToEndId/REF 2102231XX4/ /BICFI/XXXXXRA AXXX.

Obviously * is greedy, so I tried ^/InstrId/(.*?)/$, but I get an error that the expression is not valid.

Upvotes: 1

Views: 592

Answers (2)

The fourth bird
The fourth bird

Reputation: 163577

You could change the .* to a negated character class [^/]* not crossing matching /

If you have to match the whole line, then you can use .* and end the pattern with a forward slash at the end.

^/InstrId/([^/]{1,16}).*/$

The pattern matches:

  • ^/ Match / at the start of the string
  • InstrId/ Match literally
  • ([^/]{1,16}) Capture group 1, match 1-16 chars other than /
  • .* Match the rest of the line
  • /$ Match / at the end of the line

See a regex demo

Upvotes: 0

tripleee
tripleee

Reputation: 189809

$ requires that the match is anchored to the end of the line.

You probably want simply

^/InstrId/([^/]*)

which matches as may non-slash characters as possible.

If you want to limit the length, you can match exactly 16 non-slash characters:

^/InstrId/([^/]{16})

If the string could sometimes be shorter than 16 characters, try

^/InstrId/([^/]{1,16})

which matches as many as possible, up to a maximum of 16.

If you want to require exactly 16 digits, possibly prefixed with non-digit, non-slash characters,

^/InstrId/([^/0-9]*[0-9]{16})

Perhaps review the Stack Overflow regex tag info page which covers some common beginner questions and has links to more learning resources.

Upvotes: 0

Related Questions