By Magaesh


2019-06-11 14:46:36 8 Comments

Let's say I have word phone

It's possible matches in my case are as follows

  1. phone (no space in the beginning and in the ending just phone)
  2. "phone" (can have special characters at the end or in the beginning)

Cases to be Neglected [Here I'll mark the space with \s]

  1. phone\s (any space in either in the beginning or in the end should not be matched)
  2. phoneno (any alphabets or numbers appended with phone should not be matched)

I've tried the following regex [^\w\s]items[^\w\s] link

But It didn't match the case of phone with no space in the beginning and the end as it requires 1 letter other than space and alphabets in the beginning and the end

Kindly suggest any other solutions which satisfies above mentioned cases

You can find the regex here

1 comments

@Wiktor Stribi┼╝ew 2019-06-12 07:59:16

You may use custom word boundaries, a combination of \b and (?<!\S) / (?!\S):

(?<![\w\s])phone(?![\w\s])

See the regex demo and the regex graph:

enter image description here

The (?<![\w\s]) negative lookbehind pattern matches a location in string that is NOT immediately preceded with a word or whitespace char.

The (?![\w\s]) negative lookahead pattern matches a location in string that is NOT immediately preceded with a word or whitespace char.

Related Questions

Sponsored Content

11 Answered Questions

[SOLVED] How to negate specific word in regex?

  • 2009-08-06 17:20:45
  • Bostone
  • 616300 View
  • 572 Score
  • 11 Answer
  • Tags:   regex

9 Answered Questions

[SOLVED] Regex for string not ending with given suffix

  • 2013-05-06 12:09:51
  • Menno
  • 152715 View
  • 150 Score
  • 9 Answer
  • Tags:   regex

1 Answered Questions

[SOLVED] How to find words with all uppercase letters without space?

  • 2017-09-05 09:46:21
  • Tom Kurushingal
  • 592 View
  • 1 Score
  • 1 Answer
  • Tags:   regex sublimetext3

2 Answered Questions

[SOLVED] Match word followed by space and N number of digits

  • 2017-03-29 23:57:53
  • Davita
  • 4601 View
  • 2 Score
  • 2 Answer
  • Tags:   java android regex

12 Answered Questions

[SOLVED] Can you provide some examples of why it is hard to parse XML and HTML with a regex?

  • 2009-03-31 14:13:39
  • Chas. Owens
  • 41407 View
  • 394 Score
  • 12 Answer
  • Tags:   html xml regex

0 Answered Questions

Regex don't match word boundary if word starts with accented letter

  • 2017-02-13 12:46:46
  • Marcos Fernandez
  • 242 View
  • 1 Score
  • 0 Answer
  • Tags:   php regex utf-8

7 Answered Questions

[SOLVED] Greedy vs. Reluctant vs. Possessive Quantifiers

  • 2011-03-16 00:55:41
  • Regex Rookie
  • 86459 View
  • 333 Score
  • 7 Answer
  • Tags:   regex regex-greedy

3 Answered Questions

[SOLVED] Unicode equivalents for \w and \b in Java regular expressions?

2 Answered Questions

[SOLVED] Match word with Regex (at the beginning, the end or in the middle) of text

  • 2015-03-28 15:21:41
  • wanted
  • 1737 View
  • 2 Score
  • 2 Answer
  • Tags:   regex

3 Answered Questions

[SOLVED] Javascript Regex match any word that starts with '#' in a string

  • 2012-11-25 18:45:14
  • SimplGy
  • 18172 View
  • 8 Score
  • 3 Answer
  • Tags:   javascript regex

Sponsored Content