• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

Regex Help

I have a string like this

[ 123] : 01/01/07 This is a test line

How can I use regex to extract the number from the []. Number of digits varies.

  • 2
1 Solution
K VDatabase ConsultantCommented:
The pattern \d matches any single digit.
so you have number of digits in [] predefined? i mean if its 3 then u can use it so
K VDatabase ConsultantCommented:
$mystring = "[2004/04/13] The date of this article.";
@myarray = ($mystring =~ m/(\d+)/g);
print join(",", @myarray);

Prints "2004,04,13". This does the same thing as before, except assigns the returned values from the pattern search into myarray.

This gets whatever digits are between the [ and ]

This gets whatever (digits or otherwise) are between the [ and ]
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now