سؤال

I'm trying to parse through a bunch of PHP code using ack. I'm pretty inexperienced with regular expressions and trying to figure out why what I'm using currently won't work as I expect.

I am trying to find all lines in PHP files that match a pattern of:

The string "SKU", followed by any amount of any character, followed by an actual SKU (sequence of numbers of indeterminate length).

My regexp is currently:

SKU.*[0-9]*

but this matches a lot of strings which don't really fit the pattern I'm seeking, such as

SKUs</h3> and SKU',

I also tried:

SKU(.*)([0-9]*)

to no avail.

Any gurus out there have some insight? Thanks in advance.

هل كانت مفيدة؟

المحلول

All (almost) right:

SKU.*[0-9]+

You must use + instead of * if you want to have at least one number at the end of re.

To say truth that is no fully clear what do you mean with any characters. If you really mean any character then you must use .*, but if not you must say more concrete what you want to find.

Also, it's possible that you will need non-greedy mode of search:

SKU.*?[0-9]+

That means: stop as soon as you face first number.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top