Set a group in regex

Asked

Viewed 140 times

2

I’m testing the following regex:

(?i)(\s|\W|)(fire|fire-type)(\s|\W)

I have 2 problems:

1º Case (SOLVED) has only one word without spaces at the beginning or at the end my regex does not take. Ex:

fire

2nd In my group I have a fire-type but the regex only takes the word fire and igonara the -type. Does anyone know how I can solve these 2 cases?

  • What is the purpose of Regex? Is to find the words in a text fire and fire-type?

  • That’s right... @David

  • 3

    (fire\-type)|(fire) resolves ?

  • 1

    Reply that I valid, it worked...@David

1 answer

5


Usually in regular expressions the Hyphen - is a sequence indicator, for example, rather than writing [abcdefgh], we write, [a-h]. Or capitalize and minuscule with, [a-hA-H].

This way the Hyphen is character used to define sequences in regular expressions, if you want to locate a text with the hyphen we should warn the regex that they should be understood as literal preceding them with an escape signal \ (backslash), below some commonly used examples:

\- (faz a busca pelo "-")
\] (faz a busca pelo "]")
\\ (faz a busca pelo "\")
  • Show, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.