2

I am just a beginner to regex, can you help me with this?
How can I search for Feirense in the Chaves v Feirense string?

Comments
  • 1
    For a simple word search you can just type it out

    "Foo bar baz quux"
    /bar/

    Only bar will get matched.
    With a regex like /boo/, you will not get any match
  • 0
    @GodlikeBlock but if the string is "Foo Bar Bat"

    And you wanna match for "Bar BT" from the above string
    It should return true because the Bar is matching
  • 1
    As @GodlikeBlock said, just using Feirense as the pattern will match just that. However, even though that's the simplest case for a regex it's also probably the least useful one. You'd be better off just doing a strpos()/indexOf()/whatever, it's simpler and faster.

    Regular expressions are more meant for things like “one or more uppercase letters followed by one of these symbols, an optional space and then either this string or three or more characters that aren't any of these…“.

    Take a look at this site, it's good for learning and practicing regex: https://regexcrossword.com/
  • 0
    @Ganofins No, if the pattern is exactly “Bar BT” it'll only match exactly that string: uppercase B, a, r, a space, uppercase B and uppercase T, exactly in that order.

    You can make the pattern case insensitive by using the i flag, but still the letter ‘a’ in the middle of the word “Bat” prevents the pattern from matching.

    If you'd want to match the word “Bat” followed by a space, a letter B, any letters in between and then a letter T, you could use the following pattern:

    Bat B[a-z]*T
  • 0
    @ethernetzero I already tried using strpos() but that's not working

    Note:- String is Chaves v Feirense and I am matching for Feirense FC
    It returns false if I use strpos()
    I wanna return true for that
    If their is any regex method for this...??
  • 1
    @Ganofins I'm afraid you'd be out of luck with that pattern. You're trying to look for the string “Feirense FC” inside the string “Chaves v Feirense”, which doesn't include the “FC” part. If you're okay with the “FC” part being optional, you could use the following pattern:

    Feirense( FC)?

    That question mark means that the sequence “space, F, C” inside the parentheses can appear zero or one time in the string. Or in other words, it's optional.
  • 0
    @ethernetzero oh...thanks btw
Add Comment