skip to Main Content

Sample text:

1. Medium Size +CA $33.00
2. Small Size -CA $4.00

I would like the result to be just ‘Medium Size’ or ‘Small Size’ using preg_match

So the regex should detect the ‘-‘ or ‘+’ sign and just give me the text output without the prices/currency

Tried

(S+)

(S+s+)

2

Answers


  1. This regexp should extract the title between the first field (1.) and last two fields separated by spaces (+CA $33.00):

    /^S+ (.+) S+ S+$/

    To capture all the titles at once, use preg_match_all() and m flag:

    <?php
    
    $txt = '1. Medium Size +CA $33.00
    2. Small Size -CA $4.00
    3. Extra-Large Size -CA $44.00';
    
    preg_match_all('/^S+ (.+) S+ S+$/m', $txt, $m);
    
    print_r($m[1]);
    
    Login or Signup to reply.
  2. Using php, you can make use of K to forget what is matched so far, and then get a match only for the part before the + or - sign

    ^d+.h+K[^n+-]*[^s+-]
    

    Explanation

    • ^ Start of string
    • d+. Match 1+ digits followed by a .
    • h+K Match 1+ horizontal whitespace chars, and then forget what is matched so far using K
    • [^n+-]* Match optional char other than + or - or a newline
    • [^s+-] Match at least a single non whitespace character other than + or -

    Regex demo

    If there should be a + or - sign present, you can assert it:

    ^d+.h+K[^n+-]*[^s+-](?=h*[+-])
    

    Regex demo

    Or as an alternative a bit broader match starting with S+ and capturing as few as possible characters in group 1 until the first occurrence of 1+ spaces followed by either + or -

    ^S+h+(.+?)h+[+-]
    

    Regex demo

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