Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression
Help
--------------------------------------------------------------------------------
I need help writing a regular expression that only returns part of a
string.
For Example I have a multi-line text fragment like below:
PC ADVISORS, LC
1234 MT. PARMA ROAD. SUITE A1
ATLANTA, GA 30097
PH. (404) 555-1212
I write a regular express that retrieves the second line. I want to
only return all the data before SUITE A1 in the line. The regular
expression I have so far is "\n\n.*?! S" . This returns "1234 MT.
PARMA ROAD. S" . Could anybody help me figure out how to write a
regular expression that only returns "1234 MT. PARMA ROAD." . The
stipulation is that the first part of the line is a variable length.
Thanks,
John
|
|
0
|
|
|
|
Reply
|
jsteskal (1)
|
7/11/2007 3:52:51 PM |
|
"Mr.Steskal" <jsteskal@recidev.com> wrote in message
news:1184169171.422090.66130@w3g2000hsg.googlegroups.com...
> Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression
> Help
>
> --------------------------------------------------------------------------------
>
> I need help writing a regular expression that only returns part of a
> string.
>
> For Example I have a multi-line text fragment like below:
>
> PC ADVISORS, LC
> 1234 MT. PARMA ROAD. SUITE A1
> ATLANTA, GA 30097
> PH. (404) 555-1212
>
> I write a regular express that retrieves the second line. I want to
> only return all the data before SUITE A1 in the line. The regular
> expression I have so far is "\n\n.*?! S" . This returns "1234 MT.
> PARMA ROAD. S" . Could anybody help me figure out how to write a
> regular expression that only returns "1234 MT. PARMA ROAD." . The
> stipulation is that the first part of the line is a variable length.
Will it always end in "SUITE xyz?" If so, use something like "(\n\n.*?!)
SUITE" and reference the sub-match.
|
|
0
|
|
|
|
Reply
|
David
|
7/11/2007 5:22:15 PM
|
|
Mr.Steskal wrote on 11 jul 2007 in comp.lang.javascript:
> Posted: Wed Jul 11, 2007 7:01 am Post subject: Regular Expression
> Help
>
> -----------------------------------------------------------------------
> ---------
>
> I need help writing a regular expression that only returns part of a
> string.
>
> For Example I have a multi-line text fragment like below:
>
> PC ADVISORS, LC
> 1234 MT. PARMA ROAD. SUITE A1
> ATLANTA, GA 30097
> PH. (404) 555-1212
>
> I write a regular express that retrieves the second line. I want to
> only return all the data before SUITE A1 in the line. The regular
> expression I have so far is "\n\n.*?! S" . This returns "1234 MT.
> PARMA ROAD. S" . Could anybody help me figure out how to write a
> regular expression that only returns "1234 MT. PARMA ROAD." . The
> stipulation is that the first part of the line is a variable length.
<script type='text/javascript'>
var t = 'PC ADVISORS, LC\n1234 MT. PARMA ROAD. SUITE A1\n'+
'ATLANTA, GA 30097\nPH. (404) 555-1212'
t = t.replace(/(^.*?\n)|( suite[\s\S]*)/ig,'')
alert(t)
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
|
|
0
|
|
|
|
Reply
|
Evertjan
|
7/11/2007 5:39:39 PM
|
|
In comp.lang.javascript message <1184169171.422090.66130@w3g2000hsg.goog
legroups.com>, Wed, 11 Jul 2007 08:52:51, Mr.Steskal
<jsteskal@recidev.com> posted:
>For Example I have a multi-line text fragment like below:
>
>PC ADVISORS, LC
>1234 MT. PARMA ROAD. SUITE A1
>ATLANTA, GA 30097
>PH. (404) 555-1212
The first question must be whether SUITE... will always be present.
Then, if it is not, what should the result be.
In practice, it will make little difference; but, for readability, it
might be better to use .match when the underlying intention is to copy a
fragment and .replace when it is to edit the contents.
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.bancoems.com/CompLangPascalDelphiMisc-MiniFAQ.htm> clpdmFAQ;
<URL:http://www.borland.com/newsgroups/guide.html> news:borland.* Guidelines
|
|
0
|
|
|
|
Reply
|
Dr
|
7/11/2007 10:15:22 PM
|
|
On Jul 11, 3:15 pm, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
> In comp.lang.javascript message <1184169171.422090.66...@w3g2000hsg.goog
> legroups.com>, Wed, 11 Jul 2007 08:52:51, Mr.Steskal
> <jstes...@recidev.com> posted:
>
> >For Example I have a multi-line text fragment like below:
>
> >PC ADVISORS, LC
> >1234 MT. PARMA ROAD. SUITE A1
> >ATLANTA, GA 30097
> >PH. (404) 555-1212
>
> The first question must be whether SUITE... will always be present.
> Then, if it is not, what should the result be.
>
That's a good question, among others with regard to the specification.
One might assume that if "SUITE" is not present, the line itself
should be selected, but of course only the OP can say for certain. In
that event
\n((?!\s+SUITE).)+
would be one way to provide some selection flexibility (albeit with \n
included in the match).
> In practice, it will make little difference; but, for readability, it
> might be better to use .match when the underlying intention is to copy a
> fragment and .replace when it is to edit the contents.
And that's good advice, as general rule.
However, this is a case where context (the second line) beyond the
selection target is required in order to determine the appropriate
point in the string to apply the matching expression. That means there
will be excess character(s), e.g. "\n", in the matches that usually
require removal in some fashion.
Where context needs to be established in the matching process, ".exec"
can be better used (most often within a loop) to capture the
appropriate substrings matched by the expression. Nonetheless, if a
single selection is to be made, then .replace may be the correct
choice.
--
../rh
|
|
0
|
|
|
|
Reply
|
ron
|
7/12/2007 4:00:26 AM
|
|
|
4 Replies
85 Views
(page loaded in 0.079 seconds)
|