Extracting a substring from a large string in Erlang

I need to find a substring in a string and return it if it is in the string. What is the best way to do this in Erlang? Note that I donโ€™t know the place that the substring occurs in the larger line, so I need to do a search.

+4
source share
2 answers

You can use regex:

> 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 another capture option is right for you.

Or, if you do not need all the functions of regular expressions, string: str / 2 might be enough:

 > string:str(" Hello Hello World World ", "Hello World"). 8 
+8
source

This little feature can help you. It returns true if a small string can be found in a large string, otherwise it returns false.

 string_contains(Big, Small)-> string:str(Big, Small) > 0. 
+3
source

Source: https://habr.com/ru/post/1447472/


All Articles