skip to Main Content

I have been trying to write regular expression which find a specific word at specific position in javascript adobe classification rule builder

DI_NEO_OP_BRA_CRC_LRN_DIS_EP_NA_CON_UC_NA  

I want to get result as CON from above line in adobe classification rule builder code which i have been using to get the desired output is

^(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)_(.+?)$

however this code only give output as any word which is at 10th position? I want to get output which matches CON and is at 10th position.

2

Answers


  1. (?<=              # Match after
      ^               # the beginning of the line, then
      (?:[^_]+_)      #         1+ non-underscore chars followed by '_',
      {9}             # 9 times
    )                 # 
    CON               # literal 'CON',
    (?=_|$)           # followed by '_' or end of line.
    

    Try it on regex101.com.

    Login or Signup to reply.
  2. You can use a single capture group, and then match CON followed by either _ or the end of the string:

    ^(?:[^_n]*_){9}(CON)(?:_|$)
    

    Explanation

    • ^ Start of string
    • (?:[^_n]*_){9} Repeat 9 times matching any character except _ or a newline, and then match _
    • (CON) Capture group 1, match CON
    • (?:_|$) Match either _ or assert the end of the string

    Regex demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search