From: Neel
Subject: Variables in regular expressions
Date: 
Message-ID: <923666cb.0208151425.13dcd61e@posting.google.com>
Hi:

Would it be possible to have variables in a regular expression?

Say I have a variable temp which holds the string "wxyz abc"
and another variable var which is determined at run-time. How would I
search "temp" for a string that begins with the value held by "var"

Your help is appreciated.

--Neel

From: Edward O'Connor
Subject: Re: Variables in regular expressions
Date: 
Message-ID: <uptwjpxip.fsf@oconnor.cx>
> Say I have a variable temp which holds the string "wxyz abc" and
> another variable var which is determined at run-time. How would
> I search "temp" for a string that begins with the value held by
> "var"

Hmm.

How about somethign like this?

(let ((temp "wxyz abc")
      (var "floobin?"))
  (string-match (regexp-quote var)))


Ted

-- 
Edward O'Connor
···@oconnor.cx
From: Edward O'Connor
Subject: Re: Variables in regular expressions
Date: 
Message-ID: <uit2bpxf4.fsf@oconnor.cx>
> Say I have a variable temp which holds the string "wxyz abc" and
> another variable var which is determined at run-time. How would
> I search "temp" for a string that begins with the value held by
> "var"

[Previous response canceled for not-thinking-before-posting reasons]

Would something like this work?

(let ((temp "wxyz abc")
      (var "floobin?))
  (string-match (regexp-quote var) temp))


Ted

-- 
Edward O'Connor
···@oconnor.cx
From: Michael Slass
Subject: Re: Variables in regular expressions
Date: 
Message-ID: <m3u1lv91ue.fsf@thneed.na.wrq.com>
··········@hotmail.com (Neel) writes:

>Hi:
>
>Would it be possible to have variables in a regular expression?
>
>Say I have a variable temp which holds the string "wxyz abc"
>and another variable var which is determined at run-time. How would I
>search "temp" for a string that begins with the value held by "var"
>
>Your help is appreciated.
>
>--Neel

,----[ C-h f string-match RET ]
| string-match is a built-in function.
| (string-match REGEXP STRING &optional START)
| 
| Return index of start of first match for REGEXP in STRING, or nil.
| Case is ignored if `case-fold-search' is non-nil in the current buffer.
| If third arg START is non-nil, start search at that index in STRING.
| For index of first char beyond the match, do (match-end 0).
| `match-end' and `match-beginning' also give indices of substrings
| matched by parenthesis constructs in the pattern.
`----

REGEXP is a string representing the regular expression, or it can be a
variable containing a string representing the regular expression, or
it can be a function returning a string representing the regular
expression.

so ...

(string-match var temp)

-- 
Mike Slass

,----                       WRQ, Inc.
|
| We specialize in integration software and services that let you
| quickly adapt your host-intensive environment to meet new business
| needs.
`----