-2

I have a paragraph which contains i++ and ++i:

para = "This is what i++ is do ++i and in i I dim ++ +"

and I want to find all occurrences of i++ and ++i. As I am a total beginner at this so this is what I write at regex101.com

[i+]

This works fine but as you can see it also gives me all the I i + ++ everywhere in the string so I added this \b[i+]\b but it doesn't select the + sign attached with i++ . Also, it selects the single i in the string. Which means it is not treating i++ or ++i as a single string.

How can I change my regex to select i++ or ++i in the string

Raghav Patnecha
  • 716
  • 8
  • 29

1 Answers1

1

You need to escape your + signs.

You can use the RegEx i\+\+|\+\+i

  • i\+\+ matches i++ literally

  • \+\+i matches ++i literally

Demo.

Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • OP doesn't need to escape `+` in his / her own regex. – revo Apr 17 '18 at 08:41
  • @revo Whether `+` on its own needs to be escaped depends on the regex syntax in use. The OP hasn't specified, but is using regex101.com, in which case `+` is a metacharacter and does require `+` to be escaped to be taken literally. –  Apr 17 '18 at 08:42
  • @hvd Please let me know which regex flavor treats `+` as a special character in a character class. – revo Apr 17 '18 at 08:44
  • `\+\+i|i\+\+` and `i\+\+|\+\+i` both worked . Can you tell me what is the difference – Raghav Patnecha Apr 17 '18 at 08:46
  • @revo Is that relevant? The first thing the OP needs to do is get rid of the character class. –  Apr 17 '18 at 08:46
  • @RaghavPatnecha There is none, `|` only means `OR`. The order doesn't matter. – Zenoo Apr 17 '18 at 08:47
  • @hvd It's relevant since it was the subject of my first comment which you criticized. – revo Apr 17 '18 at 08:48
  • @revo Okay. In that case, let me respond to your first comment again: is that relevant? The fact that the OP doesn't need to escape `+` in a character class doesn't change the fact that the OP does need to escape `+` outside of a character class, and in this answer, `+` is no longer in a character class. –  Apr 17 '18 at 08:49
  • @hvd Do not mix-up over different points, please. This answer says *You need to escape your `+` signs.*, no, OP doesn't need to escape his / her `+` signs unless they use this solution. That sentence doesn't apply simply. – revo Apr 17 '18 at 09:00
  • @revo I guess I'm giving both Zenoo and the OP the benefit of the doubt, that the only reason that the OP used `[i+]` in the first place was because `i+` wasn't matching any `+` characters. Under that assumption, this answer is correct. But if that assumption is incorrect, yes, your point is valid. –  Apr 17 '18 at 09:03
  • @revo Looking at OP's example and him explicitly saying `I want to find all occurrences of i++ and ++i `, I think we can safely assume the answer is indeed matching OP's needs. – Zenoo Apr 17 '18 at 09:05
  • @Zenoo Solution is correct (I commented it too under question) but first statement is not. It's a matter of rephrasing. – revo Apr 17 '18 at 09:09