user3156386
user3156386

Reputation: 157

Regex expression to search a word followed by any sign of a certain set

Ik want to write a regex expression to check strings for words followed by a ?,:,! but I tried so many things and nothing seems to work properly.. The best I came up with is: \\b([a-zA-Z]*)([\:]|[\?])\

The test phrase? is something: like this! to test whether everything? works.

I am writing a function that come's from the example sentence above to the following result:

array(
[0] => phrase,
[1] => array(
             [0] => something, 
             [1] => like this,
[2] => everything
)

Upvotes: 1

Views: 57

Answers (2)

KeyNone
KeyNone

Reputation: 9150

I'd go for

(\w+)[?!:]

if you just want to make sure a word is followed by one of ?!:. No need for a lookahead, checks for word boundaries or other things (always remember: keep it simple and stupid!).


Regarding your comment on @275365s answer:

a regex that returns just the single word if the words has a ? behind it and that returns several words when there is a word with a ':'? When there is a word with a ':', I want to caputure that word and every following word until the next !

This is perfectly possible:

(\w+)\?|(\w+):\s?(.+?)!

Regular expression visualization

Debuggex Demo
Demo @ regex101

Again: keep it simple and stupid, but note that this will fail if there are nested :?! inside a :abc! block!
For example the sentence i: will? fail! won't give you the single will, it will give you i and will? fail.

Explanation:

  (\w+)\?   #every word which is followed by a ?
|           #OR
  (\w+)    #every word
  :         #which is followed by a : 
  \s?       #and an optional whitespace
  (.+?)     #every character
  !         #until a ! is encountered

Upvotes: 0

Justin O Barber
Justin O Barber

Reputation: 11591

You might try this instead:

\b\w+\b(?=[!:?])

This will produce the following result for your test sentence:

'phrase', 'something', 'this', 'everything'

You have an initial word boundary (\b), but you do not complete the word before looking for a !, :, or ?. These punctuation marks will not match within a given word.

Further, [!:?] will by default just match any one of those three punctuation marks, so you don't need an or (|).

If you are looking to match every full phrase/sentence before those three punctuation marks, you could try a regular expression like this:

\w[\w\s]+?(?=[!:?])

This would give you:

'The test phrase', 'is something', 'like this', 'to test whether everything'

Upvotes: 2

Related Questions