Help with a Regular Expression

M

Mr.Steskal

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
 
E

Evertjan.

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>
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
legroups.com>, Wed, 11 Jul 2007 08:52:51, Mr.Steskal
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.
 
R

ron.h.hall

In comp.lang.javascript message <[email protected]
legroups.com>, Wed, 11 Jul 2007 08:52:51, Mr.Steskal



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.
 

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

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top