Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a substring from a large string in Erlang

Tags:

string

erlang

I need to search for a substring in a string and return that if it is there in the string. What is the best way to do that in Erlang? Note that i dont know the place that substring happens in the bigger string so i need to do a search for that.

like image 268
coffeMug Avatar asked Dec 20 '22 13:12

coffeMug


2 Answers

You can use a regular expression:

> re:run("foobarbaz", "bar", [{capture, first, list}]).
{match,["bar"]}

See the documentation for re:run/3 for more information. In particular you may find that a different capture option suits your need.

Or if you don't need all the features of regular expressions, string:str/2 might be enough:

> string:str(" Hello Hello World World ", "Hello World").
8
like image 63
legoscia Avatar answered Jan 18 '23 22:01

legoscia


This small function may help you. It returns true if the small string can be found in the big string, otherwise it returns false.

string_contains(Big, Small)->
    string:str(Big, Small) > 0.
like image 26
David Wickstrom Avatar answered Jan 18 '23 22:01

David Wickstrom