regex matching a state in a string

  • Follow


  I wonder if there is an easier way to do this. State here is going
to be two characters,
it can be preceded by space, or comma and followed by period, end of
string, or space.
Also, if this can be done with an SQL select like pattern please let
me know

      pat = /\s{state.to_s.upcase}\s/
      pat2 = /\s{state.to_s.upcase}$/
      pat3 = /,{state.to_s.upcase}$/
      pat4 = /,{state.to_s.upcase}\s/
      pat5 = /,{state.to_s.upcase}\./
      pat6 = /\s{state.to_s.upcase}\./

    if pat.match(station.description) or
      pat2.match(station.description) or
      pat3.match(station.description) or
      pat4.match(station.description) or
      pat5.match(station.description) or
      pat6.match(station.description)
0
Reply jrubiando (15) 3/15/2012 9:29:39 PM

Jedrin=E6=96=BC 2012=E5=B9=B43=E6=9C=8816=E6=97=A5=E6=98=9F=E6=9C=9F=E4=BA=
=94UTC+8=E4=B8=8A=E5=8D=885=E6=99=8229=E5=88=8639=E7=A7=92=E5=AF=AB=E9=81=
=93=EF=BC=9A
> I wonder if there is an easier way to do this. State here is going
> to be two characters,
> it can be preceded by space, or comma and followed by period, end of
> string, or space.
> Also, if this can be done with an SQL select like pattern please let
> me know
>=20
>       pat =3D /\s{state.to_s.upcase}\s/
>       pat2 =3D /\s{state.to_s.upcase}$/
>       pat3 =3D /,{state.to_s.upcase}$/
>       pat4 =3D /,{state.to_s.upcase}\s/
>       pat5 =3D /,{state.to_s.upcase}\./
>       pat6 =3D /\s{state.to_s.upcase}\./
>=20
>     if pat.match(station.description) or
>       pat2.match(station.description) or
>       pat3.match(station.description) or
>       pat4.match(station.description) or
>       pat5.match(station.description) or
>       pat6.match(station.description)

I think this would help:

pat =3D /(\s|,)#{state.to_s.upcase}(\s|\.|$)/

if station.description =3D~ pat
0
Reply mars.90226 (1) 3/16/2012 3:16:14 PM


On Friday, March 16, 2012 4:16:14 PM UTC+1, mars....@gmail.com wrote:
> Jedrin=E6=96=BC 2012=E5=B9=B43=E6=9C=8816=E6=97=A5=E6=98=9F=E6=9C=9F=E4=
=BA=94UTC+8=E4=B8=8A=E5=8D=885=E6=99=8229=E5=88=8639=E7=A7=92=E5=AF=AB=E9=
=81=93=EF=BC=9A
> > I wonder if there is an easier way to do this. State here is going
> > to be two characters,
> > it can be preceded by space, or comma and followed by period, end of
> > string, or space.
> > Also, if this can be done with an SQL select like pattern please let
> > me know
> >=20
> >       pat =3D /\s{state.to_s.upcase}\s/
> >       pat2 =3D /\s{state.to_s.upcase}$/
> >       pat3 =3D /,{state.to_s.upcase}$/
> >       pat4 =3D /,{state.to_s.upcase}\s/
> >       pat5 =3D /,{state.to_s.upcase}\./
> >       pat6 =3D /\s{state.to_s.upcase}\./
> >=20
> >     if pat.match(station.description) or
> >       pat2.match(station.description) or
> >       pat3.match(station.description) or
> >       pat4.match(station.description) or
> >       pat5.match(station.description) or
> >       pat6.match(station.description)
>=20
> I think this would help:
>=20
> pat =3D /(\s|,)#{state.to_s.upcase}(\s|\.|$)/
>=20
> if station.description =3D~ pat

Why not just

if /\b#{state.to_s.upcase}\b/ =3D~ station.description
....
end

\b matches a word boundary which will match at beginning and end of string =
as well as transitions between punctuation or whitespace and word character=
s.

Kind regards

robert
0
Reply shortcutter (5781) 3/26/2012 8:48:08 AM

2 Replies
74 Views

(page loaded in 0.168 seconds)

Similiar Articles:













7/8/2012 8:29:18 AM


Reply: