Re: regular expression help

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

Work through this and you should get the idea:

PS C:\Documents and Settings\Andrew Watt> "the 123" -match "(\w{3})
(\d{2})"
True
PS C:\Documents and Settings\Andrew Watt> $matches

Name Value
---- -----
2 12
1 the
0 the 12


Watch for linebreaks in the first command. There is a space character
in the middle of the pattern to be matched.

The "True" says there is one (or more) matches. Use $matches to see
all matches (which includes captures in this case).

You use paired parentheses to capture. Use $matches[n] to view/use the
captures.

$matches[1] holds "the" and $matches[2] holds "12" in the example.

If you want detailed help on .NET regex syntax start from
http://msdn2.microsoft.com/en-us/library/az24scfc.aspx.

I hope that helps.

Andrew Watt MVP

On 9 Aug 2006 15:48:14 -0700, "dg" <drewg1010101@xxxxxxx> wrote:

Hi
Thanks for responding
I need to capture a character sequence to do something with it. the
file i have is munged by numbers abutting characters, so that happens
in a few places, like after a person's name, or after the state
(followed by a zip code) or in the case of a phone number that falls
after the zip code.
joe blow123 Main St Spring ValleyNY10944845-775-8383

what i want it to be is

joe blow, 123 Main St Spring Valley, NY, 10944 845-775-8383

what i'm halucinating is that the first case (joe blow123) is
discernable by digits
likewise for NY10944, except here i expect a two character state before
the digits too.
for the phone, the signal is the hyphen, so that means to count back
three digits and separate it from the zip code.

is there an analog to $1 to remember what was just parsed?
thanks a ton for your help




Andrew Watt [MVP] wrote:
Are you trying to insert a comma in two specific places or do you need
to capture a character sequence to do something with it?

If you are simply wanting to insert a couple of commas please show a
"before" and "after" pattern. It would help me understand exactly what
you want to do.

Thanks

Andrew Watt MVP

On 9 Aug 2006 12:33:52 -0700, "dg" <drewg1010101@xxxxxxx> wrote:

hi
i have a file that has data that looks like this
bob jones123 Main St Suffern NY845-333-9848
tom caroll333 Elm St New City NY845-998-3827

to fix this in Perl, in a while <> I do this:
s/(\d)/,$1/;
s/(\w\w \d\d\d-\d\d\d-\d\d\d\d$)/,$1/;

so, i find the first number, and separate it from what it abuts with a
comma, then $1 gives me back 'the number'
the second foray looks for numbers after two words that fit a phone
number pattern, and repairs that one with a comma too.

in PS, i have been trying this:
I put Joe Blow675 Main St. Ossining NY914-774-9273 into $myAddressLine
and say
$myAddressLine -replace {\d+}, $1_

which is close, but no cigar...
may i please buy a clue?
thanks
.


Quantcast