0

I have these inputs:

s1 = 'I am using c++ programming'. 

s2 = = 'I am usingc++ programming'. 

I want to check if s1 or s2 contains the exact word c++.

running this regex on both s1 and s2, it does not give any output:

x=s1 #x=s2

if re.search(r'\bc\+\+\b', x):
    print(x)

expected result: detect c++ in s1

anubhava
  • 761,203
  • 64
  • 569
  • 643
LearnToGrow
  • 1,656
  • 6
  • 30
  • 53
  • `re.search(r'\bc\+\+\B', the_string)` – dawg Mar 02 '22 at 17:29
  • You can also do `if 'c++' in the_string.split()` – dawg Mar 02 '22 at 17:31
  • 1
    Some related answers on dupe links but nature of question appear different – anubhava Mar 02 '22 at 18:50
  • @anubhava If you mean getting multiple occurrence out of a string, then there is [How can I find all matches to a regular expression in Python?](https://stackoverflow.com/questions/4697882/how-can-i-find-all-matches-to-a-regular-expression-in-python) – Wiktor Stribiżew Mar 02 '22 at 18:51
  • **Duplicate of [Regex to match a word with + (plus) signs](https://stackoverflow.com/q/3641985)** – Wiktor Stribiżew Mar 03 '22 at 07:53
  • @anubhava Nothing is split anywhere here. OP needs to "detect" the word in a string, and `re.search` is enough here. "Regex to match a word with + (plus) signs" answers this question in full. – Wiktor Stribiżew Mar 03 '22 at 07:56
  • Fair enough, I will wait for OP's comment. If OP also agrees that linked question has the answer he is looking for then I will mark it dupe myself. – anubhava Mar 03 '22 at 08:12
  • @anubhava My opinion is that nowadays, most questions regarding matching whole words are duplicates. We have answers for all possible cases now with [adaptive word boundaries](https://stackoverflow.com/questions/45145626/word-boundary-with-words-starting-or-ending-with-special-characters-gives-unexpe) , whitespace boundaries, digit boundaries, unambigous and simple word boundaries. When the problem is related to the code, there are usually dupe reasons for closing, too(like using raw string literals for `\b` to work),but those can be on-topic (especially when the solution is not evident). – Wiktor Stribiżew Mar 03 '22 at 08:20
  • 1
    Does this answer your question? [Regex to match a word with + (plus) signs](https://stackoverflow.com/questions/3641985/regex-to-match-a-word-with-plus-signs) – Zephyr Mar 12 '22 at 10:02

2 Answers2

3

You may use this regex for this using different flavors of word boundaries:

\bc\+\+\B

RegEx Demo

RegEx Details:

  • \b: Word boundary between a non-word and word character
  • c\+\+: Match c++
  • \B: Inverse of word boundary to match where \b doesn't match

Python Code:

>>> import re
>>> s1 = 'I am using c++ programming'
>>> s2 = 'I am usingc++ programming'
>>> rx = re.compile(r'\bc\+\+\B')
>>> print (rx.findall(s1))
['c++']
>>> print (rx.findall(s2))
[]
>>>
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

You could do the following

import re
x = 'I am using c++ programming'

pattern = re.compile(" c\+\+ ")
results = re.findall(pattern, x)

if results:
    print(x)

If c++ is not in the string, results will be empty.

  • 2
    please read carefully the question. We need to detect only if the exact c++ exist. if you run you example on s2, it will print s2, that is why I added `\b` – LearnToGrow Mar 02 '22 at 17:26
  • Apologies, updated now. – whaddaplaya Mar 02 '22 at 17:29
  • At least for the case where you are referring to in your question. If you also want to omit say ```I am using-c++ programming``` that won't hold obviously. – whaddaplaya Mar 02 '22 at 17:30