You can use filter
with a bit of regex:
gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> g.V.filter{it.name.matches(".*ark.*")}.name
==>marko
or with more Groovy syntactic sugar:
gremlin> g.V.filter{it.name==~/.*ark.*/}.name
==>marko
The above answer (and the accepted answer) apply to TinkerPop 2.x and the following applies to 3.x which is the version currently in wide usage:
At this time TinkerPop 3.x does not support regex as part of the Gremlin core language, however regex may be available to the specific graph system you are using (DSE Graph, JanusGraph, etc) and those graphs will supply their own libraries to extend Gremlin with a regex predicate (and/or other search options like fuzzy and tokenizations). Consult the documentation of your graph for what is available.
A graph specific extension will be the most efficient way to do a regex based search as it will rely on internal indexing functions that are optimized to handle such things, but there are methods to do in-memory sorts of regex searches through closures which are similar to the TinkerPop 2.x answer above.
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().filter{it.get().value('name').matches(".*ark.*")}
==>v[1]
Since we're using a closure, we can place whatever Java (in this case Groovy) code we want in that function. Note that for it.get().value('name')
the it
is a Groovy notation that gets refers to the current Traverser
in the stream of V()
and it holds a Vertex
object which is retrieved via get()
.
This approach will work wherever closures are accepted, but they don't work everywhere as not all graphs will support them. If you are not on the JVM (e.g. python) then you are likely submitting Gremlin scripts to a server or using bytecode based requests. If you are submitting scripts then you should be able to submit the request just as shown above. If you are using bytecode then you have to explicitly declare the lambda as a string in your traversal as shown here for Python.
Ultimately, use of closures/lambdas are not recommended as they reduce portability of your code, expose security holes, etc. If you need regex, it would be best to utilize a graph that has native support for them and a code library that contains the custom predicates specific to your programming language.