List Info

Thread: Matching digits surrounded by non-digits lazily?




Matching digits surrounded by non-digits lazily?
user name
2006-05-18 08:25:10
Hi, I'm trying to write a RegEx that will match 6 or 7
adjacent digits
surrounded by non-digits, lazily.

I currently have:

\D\d{6,7}\D

Which will match, for example "-123456*", but I
want it to match but
NOT capture the non-digits. I tried:

\D?\d{6,7}\D?

to make the non-digits optional, but this didn't work.

My ideal RegEx will return the following matches:

1234567
123456

from (for example):

Blah 1234567* blah123456) 12345 blah 12345678()blah

So that only the 6 or 7 digits are captured, and only if the
6 or 7
digits are surrounded immediately by non-digits.

Hope someone can point me in the right direction!

Plankmeister.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---

Matching digits surrounded by non-digits lazily?
user name
2006-05-18 10:30:36

The Plankmeister ha scritto:
[ CUT ]
> I currently have:
>
> \D\d{6,7}\D
>
> Which will match, for example "-123456*",
but I want it to match but
> NOT capture the non-digits.
[ CUT ]

You must use the lookahead and lookbehind assertions
Modify your regexp wuith this
(?<=\D)\d{6,7}(?=\D)


--
Andrea


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regexgooglegroups.com
To unsubscribe from this group, send email to
regex-unsubscribegooglegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )