Question

Assume i have a big paragraph, in which there are words are like found field failed fired killed (so many negative words i know!!)

Now, I want to fetch line which have words starting from fi hi or k and ends with eld or ed

How would i go about searching this pattern of word in string....??

keep in check that i am asking about word pattern in string, not string pattern

These 2 surely didn't worked

egrep "^(f[ai]|k)+(eld|ed)$"

and

egrep "\<(f|k)+(eld|ed)$\>"

I'll admit i am not a hulk of regex, doing it out of basic understanding, so any one willing to suggest a better way (with some description) is most welcome too!! :)

Was it helpful?

Solution

The regex you are probably looking for would be

"\b([fh]i|k)\w*(eld|ed)\b"

The \w* should be equivalent to [a-zA-Z0-9_]*, so that will allow any word-like characters be between requested strings.

The \b is there to ensure, that the word really starts and ends with letters you want. Otherwise you might for example match string which contains word Unfailed

Also you need to remove $ and ^ from the regex because $ means end of line and ^ the beginning of line.

OTHER TIPS

I'd use

\<(fi|hi|k)[a-zA-Z]*?(eld|ed)\>

to match the words you want.

demo @ regex101
(when you take a look at the demo: \b is the same as \<

Explanation:

\<           #beginning of word
  (fi|hi|k)  #either fi or hi or k
  [a-zA-Z]*? #zero to unlimited of a-z and A-Z
  (eld|ed)   #either eld or ed
\>           #end of word

If you want to allow numbers, dashes, underscores, ... within your words, simply add them to the character-class, for example: [a-zA-Z$_] if you want to allow $ and _, too.

You can use word boundary \b.

^.*\b(fi|hi|k)\w*(eld|ed)\b.*$
   ------------------------

This pattern would select lines that contain those words

NOTE:You need to use multiline modifier m & global modifier g

Try it here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top