Home > other >  How to capture a sentence but ignore whitespace more than 1 with regex?
How to capture a sentence but ignore whitespace more than 1 with regex?

Time:08-26

My goal is to capture this string.

      04/07          TRSF E-BANKING DB                0207/QEZDF/WSA2321                                           500.00 DB
                     TANGGAL :02/07                   Q0344XXXXX12
                                                      SALZXA YAEAS SAX

What I've tried:

  1. The regex used: (\d{2}/\d{2})([ ]{1,}[\w -]*)

However, the [\w -]* capture TRSF E-BANKING DB instead of just the TRSF E-BANKING DB.

I am not sure how to tell the regex within the match case that the whitespace length should not be more than 1.

CodePudding user response:

You can write the pattern as:

(\d{2}\/\d{2})  ([\w-] (?: [\w-] )*)

Explanation

  • (\d{2}\/\d{2}) Capture group 1, match 2 digits, / and 2 digits
  • Match 1 or more spaces
  • ( Capture group 2
    • [\w-] Match 1 word chars or -
    • (?: [\w-] )* Optionally repeat matching 1 space and 1 or more word chars or -
  • ) Close group 2

Regex demo

  • Related