Home > Software engineering >  How to write RegEx for the Below Expression
How to write RegEx for the Below Expression

Time:02-24

12345678912345678T / 14750,47932 SS
Vis à 6PC 45H Din 913 M  8x20
Art. client: 294519
QTE: 200 Pce

I want to write a RegEx which can find above stated multiline string type from a long txt file where Starting condition will be "18 digit long word" comprises with numbers and Uppercase alphabets and Ending condition shoould be "Pce"

I have written this much and it only reads first line but don't know what to write next

^[0-9A-Z]{18,18}.*

Any type of help will be appreciated.

CodePudding user response:

. in most engines doesn't include new lines, hence your match stopping at the end of the line. You could either use the DOTALL flag if available, otherwise hack around with an "include-all" class, for example [\s\S] (a char that is either a space or not a space).

With a lazy quantifier, you could use for example:

^[0-9A-Z]{18}[\s\S]*?Pce$

CodePudding user response:

You didn't specify a programming language so something like this would work:

/^[\dA-Z]{18}[^\dA-Za-z].*?Pce$/gms
  • ^[\dA-Z]{18} - start with 18 digits and/or capital letters
  • [^\dA-Za-z] - not a digit nor letter
  • .*? - anything, lazily
    • substitute with [\s\S]*? if single line modifier is not available to you
  • Pce$ - end with Pce
  • gms - global, multi line, and single line modifiers

https://regex101.com/r/RXaAT4/1

  • Related