Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find all matches in regexes with C?

Tags:

c

regex

posix

Is there a findall function in C's regex library like there is in python:

re.findall(pattern, input_string)

I have a string containing filenames like so: "path/to/file1.ics,path/file2.ics" with an undetermined number of files in the string. I want to find all the filenames (including their path) and put them in an array of strings.

I'm using the GNU regex.h library

like image 639
Alex Mohr Avatar asked Nov 30 '13 22:11

Alex Mohr


People also ask

What is regex match in C++?

std::regex_match, std::regex_replace() | Regex (Regular Expression) In C++. Regex is the short form for “Regular expression”, which is often used in this way in programming languages and many different libraries. It is supported in C++11 onward compilers.

What is regex class in C #?

Regex in C# defines a regular expression. C# Regex class offers methods and properties to parse a large text to find patterns of characters. In this article, you’ll learn how to use the .NET Regex class in C#. Regex in C# defines a regular expression. C# Regex class offers methods and properties to parse a large text to find patterns of characters.

How to split a string using regex in C #?

Here are some examples of how to split a string using Regex in C#. Let's start coding. For use, Regex adds the below namespaces for spliting a string. Split digits from a string using Regex. string Text = "1 One, 2 Two, 3 Three is good."; The above code splits the string using D+ and loops through check number and print.

How to check if a string matches a pattern?

A regular expression is used to check if a string matches a pattern or not. A regular expression or regex or regexp is a sequence of characters that defines a pattern. A pattern may consist of literals, numbers, characters, operators, or constructs.


Video Answer


2 Answers

Assuming you're using POSIX regcomp/regexec, each call to regexec will only find the first match in the string. To find the next, use the end position of the first match (the 0th entry of the regmatch_t array filled by regexec) as an offset to apply to the string before searching it again. Repeat until you have no more matches. You can write a function to do this if you want.

like image 72
R.. GitHub STOP HELPING ICE Avatar answered Oct 16 '22 10:10

R.. GitHub STOP HELPING ICE


The C standard library (as specified by ISO/IEC 9899) does not include a regular expression module, so you will need to use an external library. Good choices include regex.h from GNU libc, as detailed in /questions/635756 and PCRE, as detailed in /questions/1421785.

like image 44
Benjamin Barenblat Avatar answered Oct 16 '22 10:10

Benjamin Barenblat