I have an issue with a regular expression which just doesn't seem to work out the way I want.
Here are two examples of (different) input strings:
Of course, not having the best knowledge of regular expressions, I fail to get the bold parts of the text for different reasons. Below is my example of doing it:
preg_match("/_([^0-9])x([^0-9])_/", $input_line, $output_array);
Of course, this is not working. The expected output I want is:
You almost got it. Just add repetition and remove ^
:
/_([0-9]+)x([0-9]+)_/
^
within a character set makes the set match anything but what is inside. Aka [^0-9]
would match anything that is not a digit.
Also character sets by themselves match only one character. Aka [0-9]
matches just one digit. +
means match 1 or more times .
/(?<=_)([0-9]+)x([0-9]+)(?=_)/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.