Home > OS >  How to reduce specific Regex expression to 50 characters or less
How to reduce specific Regex expression to 50 characters or less

Time:10-30

I am trying to match strings where there are two or more of the following words: Strength, Intelligence and Dexterity, with a value of 45 or higher. This an example of a string that would return a match:

 51 to Strength
 47 to Intelligence
 79 to maximum Life
 73 to maximum Mana
28% increased Rarity of Items found
 37% to Cold Resistance

The regex expression is to be entered in a game (Path of exile). The regex string can be a maximum of 50 characters.

The fourth bird has found a solution, but the string is more than 50 characters:

 \b[45][0-9] to (?:Str|Int|Dex)[\s\S]*?\b[45][0-9] to (?:Str|Int|Dex). 

Is there a way to found a similar expression, but with 50 characters or less?

Thanks in advance!

CodePudding user response:

You can shorten it using a group and repeat that group with a quantifier, and write [0-9] as \d for example:

^(?:[\s\S]*?\b[45]\d to (?:Str|Int|Dex)){2}

The pattern matches:

  • ^ Start of string
  • (?: Non capture group
    • [\s\S]*? Match any char as few as possible
    • \b[45]\d to (?:Str|Int|Dex) Match 4 or 5 followed by a digit, to and one of Str Int Dex
  • ){2} Close the non capture group and repeat it 2 times

Regex demo

  • Related