-4

i am frustrated while trying to solve a problem. Perhaps anyone has a idea and can help me.

I have to search for a special pattern and then cut it off.

Input Data are many million rows like this:

|FIL=827|KAS=1|BON=4189|IND=1|BED=2610|LAD=8011|DAT=20140317|UHR=090500|TYP=LOGPLU|CEURO=1|BWWS=1|BDISP=1|BDRUCK=1|BSTART=1|BAH=1|BARTSCAN=1|BREPEAT=1|ZT0=TI_NUMMER|ZW0=2400850000000|ZA0=MF Sonnenblumen 2,5kg|ZT1=TI_MENGE|ZW1=1|ZT2=TI_WGR|ZW2=9|ZT3=TI_MWST|ZW3=1|ZT4=TI_PARA|ZW4=0|ZT5=TI_PARA2|ZW5=0|ZT6=TI_EPREIS|ZW6=7.99|ZT7=TI_GPREIS|ZW7=7.99|ZT8=TI_BASISDETAIL|ZW8=30|106

If there is a pattern like "ZW0=240" this part has to be shortend to "ZW0=2400850"

Any Ideas ?

Amit Joki
  • 53,955
  • 7
  • 67
  • 89

1 Answers1

0

If you want to get this part out of the entire input string and then change this one value, first extract it with

\b(ZW0=[0-9]+)

Regular expression visualization

Debuggex Demo. The word boundary (\b) is important to prevent something like ABCXYZW0=123048734 from being incorrectly matched. It could alternatively be an escaped or-pipe: (\|).

Once this value is extracted, you can eliminate "all but one on the trailing zeros" with the following replacement:

Here's the find-what term:

^(.*?[0-9]+?0)0+$

Regular expression visualization

Debuggex Demo

The end-of-line anchor ($) is critical here. Without it, it would result instead in ZW0=240. And without the reluctant quantifier (?) before the numbers (.*?), you would end up with ZW0=240085000000. This is because it would only eliminate the one remaining zero.

Alternatively, if you want to change this value, but leave the entire input string intact, use this:


Please consider bookmarking the Stack Overflow Regular Expressions FAQ for future reference. The links in this answer come from it. In particular, the section at the bottom contains a list of online testers, where you can try things out yourself.

Community
  • 1
  • 1
aliteralmind
  • 18,274
  • 16
  • 66
  • 102
  • Wow, you guys are awesome and fast. Your input works great, but perhaps i wasn't precise enough. I need to find the string starting "ZW0=240xxxxxxxx" - this number is always 13 digits. If it matches this criteria i need a replace to the first 6 digits. in this case : "ZW0=240085" - all other case shouldn't be touched. is this possible ? – user3551342 Apr 19 '14 at 11:56
  • Find what: `(240?[0-9]{3})[0-9]+$`, replace with `$1`. Best of luck! – aliteralmind Apr 19 '14 at 12:08