2010 May 21 2:06 AM
Hey Everyone,
This may be a rudimentary query...however, we will have very recently completed a ecc 6.0 upgrade and I have projects using WebDynpro, XSLT and Interactive Forms...all new to me so I have lots to learn and have to pick my poison so to speak.
Anyway, a problem came up and I'm wondering if I can use regex to solve it. I don't need a solution, just a 'yeah, that's what you can do' an example would be nice too.
The issue is that the customers send us reference numbers that have our invoice number imbeded in them. For instance, all of these are valid examples for our invoice number of 90921266.
DEDIR9092126600
0090921266
0000000000000090921266
0000090921266PP976
90921266
What I do now is cycle through the string looking for the invoice number. You know, the standard kind of +0(8), +1(8), +2(8)...etc. Not the classiest solution but it works.
Now this process has to be corrected because of some unforseen issues. My query; is this an example of a situation where regex can be used? Having ZERO experience and a complete neophyte in this area, I thought I'd ask before investing too much time.
Simple as that. Sometimes the questions are simple.
Thanks,
Greg
2010 May 21 6:28 AM
Hi Greg,
Anyway, a problem came up and I'm wondering if I can use regex to solve it. I don't need a solution, just a 'yeah, that's what you can do' an example would be nice too.
That's the spirit - so, yep, you really should really read up on [regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX.htm]. It's a powerful tool for doing any text matching or replacements and there's hardly anything you can't do.
Legibility is not always the best though with regular expressions, especially once they start getting complex. Anyhow, looking at your patterns I'm a bit puzzled, because if we're talking about a SAP invoice number I would expect 10 characters (possibly without leading zeros). So the first pattern starting with DIRDEB seems odd, because of the trailing zeros (how would one know that what part is the invoice number). So being ignorant about your exact requirements I crafted a regular expression that should extract the invoice number for your examples except for the first one (where it would include the trailing zeros) and used it with a [replace|http://help.sap.com/abapdocu_70/en/ABAPREPLACE_IN_PATTERN.htm] command (INV_STRING should hold the invoice reference):
replace first occurrence of regex '^(?:DEDIR)?0*(\d{10})(?:PP\d+)?$'
in INV_STRING with '$1'.
So your task is now to decipher this (using the first link I've given) and then come up with the appropriate pattern that matches all your invoice numbers...
Cheers, harald
2010 May 21 6:28 AM
Hi Greg,
Anyway, a problem came up and I'm wondering if I can use regex to solve it. I don't need a solution, just a 'yeah, that's what you can do' an example would be nice too.
That's the spirit - so, yep, you really should really read up on [regular expressions|http://help.sap.com/abapdocu_70/en/ABENREGEX_SYNTAX.htm]. It's a powerful tool for doing any text matching or replacements and there's hardly anything you can't do.
Legibility is not always the best though with regular expressions, especially once they start getting complex. Anyhow, looking at your patterns I'm a bit puzzled, because if we're talking about a SAP invoice number I would expect 10 characters (possibly without leading zeros). So the first pattern starting with DIRDEB seems odd, because of the trailing zeros (how would one know that what part is the invoice number). So being ignorant about your exact requirements I crafted a regular expression that should extract the invoice number for your examples except for the first one (where it would include the trailing zeros) and used it with a [replace|http://help.sap.com/abapdocu_70/en/ABAPREPLACE_IN_PATTERN.htm] command (INV_STRING should hold the invoice reference):
replace first occurrence of regex '^(?:DEDIR)?0*(\d{10})(?:PP\d+)?$'
in INV_STRING with '$1'.
So your task is now to decipher this (using the first link I've given) and then come up with the appropriate pattern that matches all your invoice numbers...
Cheers, harald
2010 May 21 6:48 AM
Hi,
the regular expressions are really powerful tool. Ask any Unix admin or Perl developer There is a really nice program called DEMO_REGEX_TOY for playing/learning/testing regular expressions. You can easily check if regex proposed by Harald is OK. I would modify it little bit to ^[[:alpha:]]0(\d)(?:PP\d+)?$ to ignore any string of characters at the beginning, not just word DEDIR. But it really depends on your requirement.
Cheers
2010 May 21 7:16 AM
Hello,
I think this should also work. Rite?
FIND FIRST OCCURRENCE OF REGEX '[1-9][0-9]{9}' in l_string RESULTS result_tab.
Regards, Chirag.
PS: DEMO_REGEX_TOY - nice program to check your regex
Edited by: Chirag Kothari on May 21, 2010 8:33 AM
2010 May 21 8:04 AM
The Wiki formatting messed up your regular expression; what you actually had typed and meant was ^\[\[:alpha:\]\]0(\d)(?:PP\d+)?$. And why so stingy, what about \[\d\]0(\d)(?:PP\d+)?$...
2010 May 21 9:17 AM
I spotted it but I was too lazy to fix it. Here we can see another beauty of regular expressions: there are many ways how to achieve same goal. You just need to choose one
Cheers
2010 May 21 12:50 PM
Thanks Everyone...looks like I'm going to be learning regex as well this year. I truly appreciate this forum and the gurus chiming in on their area of expertise.
Greg