How to select standard words using Regex?

Asked

Viewed 76 times

1

I want to validate this regex:

- regex.test.test.test.test
- -regex.test.test.test.test
- +regex.test.test.test.test
  • If you take a trace from the beginning to the word, I want this regex to be invalidated.
  • If there is no point between words within that string, let it be invalidated also.
  • That is, the trace, the point, the sign of +, is all a structure that has to be placed correctly, as above. If you change the default form, this regex will no longer be valid.

That is, it is infinite, for example, if I put thousands of dots, but only one dot is changed, that is invalidated all.

I was trying here, but the way I do it is finite and seems to contain many errors.

(-\s{1}\w+.\w+.\w+.\w+.\w+)|(-\s{1}-\w+.\w+.\w+.\w+.\w+)|(-\s{1}[+]\w+.\w+.\w+.\w+.\w+)

How do I generate the structure of this regex, it can be infinite and only valid with correct structure?

1 answer

2


An alternative is to use this regex:

^- [-+]?\w+(\.\w+)*$

If the structure is rigid, better use the markers ^ and $, which are, respectively, the beginning and end of the string. So you ensure that it only has what is in regex.

After the ^ we have the hyphen followed by a space.

Then we have [-+], which means "the character - or the character +". And the ? soon after make this stretch optional (that is, may or may not have the - or the +).

Right after we have \w+ (one or more occurrences of letters, digits or the character _).

Coming up next, we have (\.\w+)*. In parentheses we have a point followed by one or more \w. And the quantifier * means "zero or more occurrences". That is, the sequence "point followed by \w+" can repeat itself from zero to infinity times.

If you want to make me have at least one point followed by \w+, swap the * for +:

^- [-+]?\w+(\.\w+)+$

Thus, the sequence "point followed by \w+" need to appear at least once.


If you want specific quantities, exchange the + by one of the options below:

  • {1,10}: between 1 and 10 occurrences
  • {10}: exactly 10 occurrences
  • {3,}: at least 3 occurrences (no cap)

Adapt the values according to what you need. Ex:

^- [-+]?\w+(\.\w+){3,}$

Thus, the sequence "point followed by \w+" must be repeated at least 3 times.


See here an example of regex working.


Note: the dot has special meaning in regex (meaning "any character"), then if we want only the "dot" character itself, we need to write it as \.

  • 1

    Fantastic the simplicity

Browser other questions tagged

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