Home > database >  Regex for pattern with prefix
Regex for pattern with prefix

Time:07-02

I want to write regex for any of the below pattern

  1140×90.0
  軸距C(M/T)[648(534 114)×90.0]
  軸距E[643(529 114)×90.0]
  軸距E(M/T)[768(654 114)×90.0]enter code here
   軸距G[1098(984 114)×90.0]
    軸距G(M/T)[1223(1109 114)×90.0]

I am using

\[*\d*(\d\.[\d] )?(\(\d (\.[\d] )?\ \d*([\d]*\d\.[\d] )?\))?\×\d ([\d]*\.[\d] )?\]*

It is working for below patters:

    [648(534 114)×90.0]
    [643(529 114)×90.0]
    [768(654 114)×90.0]
    1098(984 114)×90.0]
    [1223(1109 114)×90.0]
    890×90.0

Now I want to prefix above any of words before above patterns

    1140×90.0
    軸距C(M/T)[648(534 114)×90.0]
    軸距E[643(529 114)×90.0]
    軸距E(M/T)[768(654 114)×90.0]
    軸距G[1098(984 114)×90.0]`enter code here`
    軸距G(M/T)[1223(1109 114)×90.0]

Please let me know how to do this

CodePudding user response:

You can use

[^\s\[\]]*\[*\d*\.?\d (?:\(\d*\.?\d \ \d*\.?\d \))?×(?:\d*\.?\d )?\]*

See the regex demo.

Details:

  • [^\s\[\]]* - zero or more chars other than square brackets and whitespace
  • \[* - zero or more [ chars
  • \d*\.?\d - an int/float number
  • (?:\(\d*\.?\d \ \d*\.?\d \))? - an optional sequence of
    • \( - a ( char
    • \d*\.?\d - an int or float number
    • \ - a char
    • \d*\.?\d - an int or float number
    • \) - a ) char
  • × - a × char
  • (?:\d*\.?\d )? - an optional sequence matching an int/float number
  • \]* - zero or more ] chars.

CodePudding user response:

Not sure if you need the capture groups, but you can prepend the pattern with a non greedy dot .*? to match all before the pattern until you encounter the pattern.

.*?\d (\.\d )?(\(\d (\.\d )?\ \d (\.\d )?\))?×\d (\.\d )?\]*

See a regex demo

Or match for example optionally any kind of letter followed by an optional part between parenthesis:

(?:\p{L} (?:\([^()]*\))?)?\[?\d (\.\d )?(\(\d (\.\d )?\ \d (\.\d )?\))?×\d (\.\d )?\]*

Regex demo

  • Related