The proposed regex replace() function in XPath 2.0 (and also XQuery 1.0)
always replaces all matching strings, i.e. as if it had the g modifier in Perl's s///g
For details see http://www.w3.org/TR/xpath-functions/#func-replace
(It does define the semantics of overlapping strings the same as perl.)
However it seems to me that always replacing all the matching strings might
cause some loss in functionality, because there is no
obvious way to get it to only do one replacement.
The suggested workaround to achieve changing only the first matching string is
to put a ^ at the start of the pattern.
So I first ask a technical question, about Perl's behavior.
Q1. Will a pattern such as s/^.../.../g
i.e. one that is anchored by a leading ^ ever change more than one matching string?
Now a question about the real consequences of the current XPath proposal.
What is a good "use case" for wanting a replace-one in addition
to a replace-all?
The best case I can think of where this does cause
a problem is a pattern to preserve any leading whitespace
(perhaps to keep the indentation the same) but replace all other
whitespace with a single blank.
The following perl _expression_ fails to do this,
s/^(\s*)(\S+)(\s+)/\1\2 /g
and so I believe that it will be very hard to do with replace().
Q2. Can you think of a better "use case"?
Assuming that there are serious problems identified
there are several ways to solve this in XPath.
Q3. What is your preference?
a. Have two functions with different names e.g. replace-first() and replace-all()
(if so please choose your preferred names from the following set:
For first: replace, replace-one, replace-first
For all: replace, replace-all
b. Change the default for replace() to mean replace first, and add a flag
named "g" to mean replace all.
(Note that there already are flags named "s" and "m" with their perl meanings.
I have access to a newer version of the spec than the one that is posted.)
c. Keep the default for replace() as meaning replace all and add a new option
(what letter?) meaning replace first.
d. Something else
Any advice will be appreciated.
Hopefully helpfully yours,
Steve
--
Steve Tolkin Steve . Tolkin at FMR dot COM 617-563-0516
Fidelity Investments 82 Devonshire St. V4D Boston MA 02109
There is nothing so practical as a good theory. Comments are by me,
not Fidelity Investments, its subsidiaries or affiliates.
_______________________________________________ Boston-pm mailing list [EMAIL PROTECTED] http://mail.pm.org/mailman/listinfo/boston-pm