Comments (7)
When you compare a string against a regex there are in general multiple matches, each one of which has multiple captured groups. These days we could represent the full complexity of the result using maps and arrays, but back in the day we chose to represent it using XML (see fn:analyze-string). It's not clear to me that the result using maps and arrays would be more usable than the result represented as XML, and in fact delivering the result in the form of the original string augmented with markup has some real benefits.
from qtspecs.
Maybe fn:analyze-string() is enough. It’s true that the XML markup could be extended in the ways i mentioned, if needed, and also that being able to use *:match makes using analyze-string a little easier. However, in practice i’ll continue to use a wrapper for getting at just the matched groups.
from qtspecs.
It would be nice indeed to have a more lightweight alternative to fn:analyze-string
(which people struggle with). The function provides much more functionality than what most users need for everyday tasks. We could keep it simple, do what (I believe) most other languages do and return a flat sequence, for example ("ab", "b")
for match-groups('xabx', '(a(b))')
?
If #37 is not dropped, we could then do:
let $date := "2010-10-10"
let $pattern := "^(\d{4})-(\d{2})-(\d{2})$"
let ($year, $month, $day) := match-groups($date, $pattern)
Currently, it would e.g. be:
let $groups := data(analyze-string($date, $pattern)/fn:match/fn:group)
let $year := $groups[1]
let $month := $groups[2]
let $day := $groups[3]
from qtspecs.
@ChristianGruen very much so. Although maybe we ended up with a "with prefix" expression (i lost track, i know i raised it as an issue!) in which case, in environments without fn predeclared,
let ($year, $month, $day) := (with prefix "fn" := "http://www.w3.org/2005/xpath-functions" return analyze-string($input, $regex)/fn:match/text())
but this is not as nice.
from qtspecs.
I have an extract function in Xidel that only returns the matched text and the third parameter lets one choose the returned capture groups
(I always thought it to be faster to only return the data one needs)
extract(
"It was in the January of 1836 that she set out.",
"(January|February|March|April...).*(\d\d\d\d)"
)
returns January of 1836
extract(
"It was in the January of 1836 that she set out.",
"(January|February|March|April...).*(\d\d\d\d)"
, 1 to 2)
returns ("January", "1836")
extract(
"It was in the January of 1836 that she set out.",
"(January|February|March|April...).*(\d\d\d\d)"
, (0,1,2,2,2,2))
returns ("January of 1836", "January", "1836", "1836", "1836", "1836")
extract(
"It was in the January of 1836 that she set out.",
"(\w+)"
, 1)
returns "It"
And my function can also return all matches together in a sequence:
extract(
"It was in the January of 1836 that she set out.",
"(\w+)"
, 1, "*")
return ("It", "was", "in", "the", "January", "of", "1836", "that", "she", "set", "out")
extract(
"It was in the January of 1836 that she set out.",
"(\w)(\w+)"
, (1,2), "*")
return ("I", "t", "w", "as", "i", "n", "t", "he", "J", "anuary", "o", "f", "1", "836", "t", "hat", "s", "he", "s", "et", "o", "ut",)
from qtspecs.
@benibela great to see you here. I think your function would be fine, although maybe 0 or -1 would be better than "", so that the argument could be specified as xs:integer
from qtspecs.
think your function would be fine, although maybe 0 or -1 would be better than "", so that the argument could be specified as xs:integer
there is more
All regex functions have a flags parameter, e.g. "i" for case insensitive.
That is where I put the *
option. Like "i*"
and it returns all matches case insensitively.
from qtspecs.
Related Issues (20)
- Names of private functions in XQuery library modules HOT 4
- Specs: new vs. updated features HOT 3
- map:get and array:get HOT 34
- Occurrence indicator meaning "exactly one" HOT 8
- In the EBNF, use explicit separator syntax
- Further improvements to BuiltInKeywords test needed HOT 3
- fn:round: `rounding-mode` → `mode`? HOT 1
- (type)switch: braces after `case` keyword HOT 2
- Unknown option: FORG0013 → XPTY0004 HOT 2
- XQFO: Editorial HOT 1
- Duplicate keys in map constructor HOT 3
- XSLT: names of functions in pattern HOT 3
- User defined operators HOT 5
- Circular dependencies: XQDY0054 vs. XPST0008 vs. optional errors HOT 5
- fn:parse-uri: Observations (cont.) HOT 4
- Missing XSLT error code
- Quantifier expressions: optional positional argument HOT 1
- fn:build-uri: Observations HOT 7
- fn:while-do: Optional error: will not terminate HOT 1
- Annotations: duplicate names HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from qtspecs.