enter image description here

What is the difference between [+-] and [-+]. It seems to me doing the same thing. And what does [A-Za-z-]+ do?

enter image description here

Or I tried

awk '/[^-]/' contries

and I expected nothing as an output. enter image description here

  • Why would you expect nothing as output in that last example? All lines contains at least one character that is not a dash. – Kusalananda Feb 17 at 12:33
  • 5
    Please do not post images of text, especially for the terminal output. – Sparhawk Feb 17 at 12:35

The text refers to the fact that to match a dash, -, in a bracketed expression, [...], the dash must occur first or last in the [...] (or just after an initial ^, as in [^-] which matches any single character that is not a dash).

There is no difference between [+-] and [-+]. Both matches either a + or a -.

The expression [A-Za-z-]+ would match multiple (more than zero, due to the + after the [...]) ASCII characters from A to Z and from a to z and dashes. The dash could also be put first as in [-A-Za-z]+.

Note that the internal two dashes in this expression denotes ranges whereas the last dash (or first in [-A-Za-z]+) denotes a literal dash character .

The + after [...] modifies the expression so that it matches one or more characters from within the [...]. Without the +, the expression would match exactly one character, and not a word with optional dashes.

The second screenshot shows that you are matching [Y-Zy-z-] and [Y-Zy-z-]+ against some data.

You get the same result for both patterns since that's the only line that contains one or more character from the set YZyz-. The character that matches is the - character in the substring -Asia.

Then you tried with [^-]. This matches on all lines, as all lines contains some character that is not a -.

  • Thank you very much and what does "more than zero" means? I think the meaning of + in [A-Za-z-]+ ? – Lukáš Altman Feb 17 at 12:06
  • 1
    @LukášAltman + modifies the meaning of the previous expression. It modifies so that it must match at least once (but possibly more). .+ would match one or more characters. This is similar to *, but * matches zero or more times. Hence, .* would match zero or more characters. Without the + in the expression in my answer, the expression would only ever match a single character, not a word with possible hyphens. – Kusalananda Feb 17 at 12:08
  • I am sorry, I do not understant. I added a picture to my question what I tried. It is not a good example? – Lukáš Altman Feb 17 at 12:25
  • 2
    @LukášAltman See updated answer. It is unclear what you are trying to achieve. – Kusalananda Feb 17 at 12:30
  • 1
    @LukášAltman ^ means the start of the line, but within [...] it negates the character class. If you want all lines that starts with a non-dash, you would use ^[^-] (in your example data, this would probably be all lines). – Kusalananda Feb 17 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.