-2

I am new to perl (beginner, learning perl for past 1 week during spare time). This is my first programming language.

I want to know how this regex []+ works in perl. I have 3 questions.

  1. What will this do: if /[\d\s\.,:\/]+/?

I learned if /.../ matches pattern.

  1. So will it match the following?

  2. And which parts of the following will not match?

    • 335.31, 312.52
    • Dave1.532
    • Path: "./1243/453 /48.1"
    • 543, 546

Edit:

This is not a duplicate of the linked question as I am specifically asking how []+ works. The answer in the linked post does not cover this.

I know what each character in the regex I have written above represents and how each character work. What I want to know is how []+ will influence the regular expression. Specifically how the + will influence the [].

One Face
  • 395
  • 3
  • 10
  • @Zaid I am a medical student. So no, this is not homework. – One Face Feb 08 '15 at 07:47
  • 1
    About your edit the first answer covers that too. `[]` is a collection of allowed chars. And the `+` afterwards means that one of the chars before must been there at least one time. – rekire Feb 08 '15 at 08:42
  • So `501.887 ,65,3.87:654/876` will match the regex I have mentioned? @rekire – One Face Feb 08 '15 at 09:59

1 Answers1

1

I suggest you use regex101.com to try the regular expression. Below is the breakdown for the expression you provided:

`[]` match a single character present in the list
`+` matches one or more of the above
`\d` match a digit [0-9]
`\s` match any white space character `[\r\n\t\f ]`
`\.` matches the character . literally
`,:` a single character in the list ,: literally
`\/` matches the character / literally

You'll get the following matches (if you run this with g- global option) - (REGEX sample - ref):

`335.31, 312.52 `
`1.532 `
`: `
`./1243/453 /48.1`
` 543, 546 `
nitishagar
  • 8,480
  • 3
  • 22
  • 36
  • I know what the characters represent. What I did not get was how the `[]` works with `+`. Will it accept any number of characters in any combination as long as I include it in the square brackets? – One Face Feb 08 '15 at 07:41
  • 1
    @CRags Yes in case of `[]` you are repeating entire container not just that character matched multiple times, so it will match combinations. – nitishagar Feb 08 '15 at 08:44
  • Thanks, got it. I will read from the website you have linked too – One Face Feb 08 '15 at 09:56