How to find a string, within a string, using Regex
Ali Abbas

Ali Abbas @realabbas

About: Tech enthusiast with the passion to develop applications that can make this world better. "Whenever I get bored, I call API's"

Location:
India
Joined:
Apr 14, 2019

How to find a string, within a string, using Regex

Publish Date: Oct 22 '19
6 11

Thanks for stopping by.

I am stuck in a problem that I'm unable to solve. The problem is like as follows

a = "The Macports"
b = "macport"
c = "The Initial Macports"
d = "The Oscar Macports Initial Time"
e = "macports"
f = "The fastest macport"

All the above are strings where the common word is macport. Just like these 6 strings I am operating on a data of almost 1000 variation like this.

I am trying out to find the strings which contains the name macport or macports.

Glad if you could give me suggestions or discuss about it, how to fix it

Comments 11 total

  • Saurabh Daware 🌻
    Saurabh Daware 🌻Oct 22, 2019

    /macport$|macport /g.test(string)

    This will check for words having macport and a space appeneded or a string that ends with macport

    for macports you can do the same thing by replacing macport with macports

    I am on mobile so didn't check it much just check if it is working and I may have missed some cases

    • Saurabh Daware 🌻
      Saurabh Daware 🌻Oct 22, 2019

      Oh I replied with the sense that you want to differentiate between macport and macports, is this what you want?

      • SavagePixie
        SavagePixieOct 22, 2019

        Oh, that's a very good point. It does change the answer.

      • Ali Abbas
        Ali AbbasOct 22, 2019

        No, i want to include macport and macports both.

  • SavagePixie
    SavagePixieOct 22, 2019

    Do you have them in an array? That'd be ideal, as you could do something like this:

    const getWords = arr => {
       const regEx = /macports?/i
       return arr.filter(x => regEx.test(x))
    }
    

    If no array, then forget about the filter, but you can still apply the same principle.

    Another way to go would be:

    string.includes("macport")
    

    Though you might have to turn the string into lower case for this one.

  • Maynard Cabalitan
    Maynard CabalitanOct 22, 2019

    in ruby if checking per string

     "The fastest macport".include?('macport')
    
  • Si
    SiOct 22, 2019

    Can you update the title of your post to better indicate what your issue is, so people searching may find the issue easier?

    • Ali Abbas
      Ali AbbasOct 22, 2019

      Okay Sure

      • Si
        SiOct 22, 2019

        I was thinking something more like:

        How to find a string, within a string, using Regex

        or something that actually summarises the issue you're wanting help for. "Can you help me" doesn't really give an initial indication of what's being asked.

Add comment