Reputation: 171
Regex may not be the correct tool for this, so if that is the case that is a good answer as well.
I'd like to find a word even if its been split by non Letter characters. For example if i'm trying to find One
, O-ne
would match, --O__n--e
would match but O--nce
would not.
To avoid the XY problem: This is for matching words in a profanity filter. This case came up and there is a fairly straightforward (if long) solution using String.toLower
, String.Indexof
and String.Replace
to remove all the non letters, replace the word with *
s and then reconstruct the original string. I was trying to find a solution using regular expressions. Here is what I've tried so far:
(.\*)(([^a-zA-Z]*)(?i)(Y)([^a-zA-Z]*)(?i)(e)([^a-zA-Z]*)(?i)(s)([^a-zA-Z]*)\b)(.\*)
It matches something like I wasn't born --Y_e--s
, but not I wasn't born --Y_e--st
.
So I added a ?
after the \b
to make the --Y_e--s
portion optional (of course the beginning (.*
) greedily matches the entire string no matter what everytime), changing it to this:
(.\*?)(([^a-zA-Z]*)(?i)(Y)([^a-zA-Z]*)(?i)(e)([^a-zA-Z]*)(?i)(s)([^a-zA-Z]*)\b)?(.\*?)
which doesn't match anything.
I'm stumped. What I'd like is this:
(Capture Group start of string) (Capture group of my Target word that might be separated by non Letter characters) (Capture Group of End of string).
Upvotes: 0
Views: 62
Reputation: 2662
Well I'm not sure which regex flavour you use but you can try this.
https://regex101.com/r/qR1aP5/1
(\S)\s*(\S+)(\S)\s*(\S+)(\S)\s*(\S+)
Match against this text
I wasn't born --Y_e--s
Upvotes: 1