开发者

Pattern matching with regex

I am a novice in regex and trying to understand it by solving small problems. So here I am with a problem which I couldn't solve (warning: it may be extremely silly). Your inputs will help me understand the concept.

I want to write a regex which will match all items in list1 but none of those from list2

list1

   pit
   spot
   spate
   slap two
   respite

list2

   pt
   Pot
   peat
   part

I was thinking like "give me all the items that starts with 开发者_如何学编程p|s|r and endswith it|ot|e|o So i wrote ^[p|s|r].*[it|ot|e|o]$ which eventually resulted in undesired result.

Thanks in advance for your inputs.


In notepad you can't do or operations (taken from Notepad++: A guide to using regular expressions and extended search mode and tested on my Notepad++ 5.9.3)

This would work in other "standard" regexes :-)

^[psr].*(it|ot|e|o)$

Try here. http://gskinner.com/RegExr/?2uudn

What were you doing was using the [] instead of the grouping (). It was equivalent to: [itoe|] (were the | was a "standard" character instead of or) and in general everything in an [] is in or :-) [ab] means a or b.


/(pit|spot|spate|slap two|respite)/.test('Pot')

This matches the words from list one, and none from list two


I feel like I must be missing something.

^(pit|spot|spate|slap two|respite)$


It depends entirely on how you categorise the differences between the lists:

/p[ioa\s]t/
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜