newbie regular expression question numbers/strings separated by :

D

djbitchpimp

I need to match either a field in the following form:

"6667"

or

"any"

or a string of this form:

"6666:6669"

with the same regular expression (without the quotes.)

I have tried a number of combinations but when I try:

/(\w+):?(\d+)?/ it only ever matches the first number before the colon.

Does anyone have any suggestions?
 
R

RedGrittyBrick

I need to match either a field in the following form:

"6667"

or

"any"

or a string of this form:

"6666:6669"

with the same regular expression (without the quotes.)

I have tried a number of combinations but when I try:

/(\w+):?(\d+)?/ it only ever matches the first number before the colon.

Does anyone have any suggestions?

/.*/

It might help if you would provide more sample data, including stuff
that shouldn't match.
 
A

axel

I need to match either a field in the following form:
"6667"
or
"any"
or a string of this form:
"6666:6669"
with the same regular expression (without the quotes.)
I have tried a number of combinations but when I try:
/(\w+):?(\d+)?/ it only ever matches the first number before the colon.

No... it also matches on the first number after the colon - only
that match will be contained in $2.

You might want to build two or more separate regular expressions and separate
them by | along the lines of:

/(?:(\d+:\d+)|(\w+))/

You will want to pick up the result in $+ rather than $1.

Axel
 
F

Fabian Pilkowski

I need to match either a field in the following form:
"6667" or "any" or a string of this form: "6666:6669"
with the same regular expression (without the quotes.)

I don't think you mean something like

/^(6667|any|6666:6669)$/

but that is, what you describes.
I have tried a number of combinations but when I try:
/(\w+):?(\d+)?/ it only ever matches the first number
before the colon.

Remember that "\w" would catch digits too.
Does anyone have any suggestions?

Take the regex I noted above and replace the hardcoded stuff. Perhaps
this could be somthing like:

m/
^ # the beginning of the string
( # followed by
[a-z]+ # "any"
| # or followed by
\d+ # "6667"
| # or followed by
\d+:\d+ # "6666:6669"
) # and end with
$ # the end of the string
/x

That's all.

regards,
fabian
 
D

djbitchpimp

I am basically going through a string and grabbing words off the
beginning, then using the $' to get the rest of the line. I am also
using the regexp memory to store the words that I split off.

For the above pattern match I ended up using:

/^\s?\$?(\d+:\d+|\w+)/

so it will match:

$HTTP_PORTS
6667
6667:6669
any
$HTTP_PORTS

Thanks for all the help.
 
T

Tad McClellan

Tim Hammerquist said:
You did read the warning about using the $' var in parlvar, right?


And you don't need it for this task anyway.

The "usual" way of whittling down a string is to use s/^stuff//
(or s/^(stuff)// ) instead of m/stuff.*/s and $'.

Sometimes m//g and \G work nicely for string-scanning too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top