CPP
#include <bits/stdc++.h>
using namespace std;
int main()
{
string subject( "My w3wiki is my "
"w3wiki none of your w3wiki" );
smatch match;
regex r( "w3wiki" );
int i = 1;
while (regex_search(subject, match, r)) {
cout << "\nMatched string is " << match.str(0) << endl
<< "and it is found at position "
<< match.position(0)<<endl;
i++;
subject = match.suffix().str();
}
return 0;
}
|
Java
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String subject = "My w3wiki is my w3wiki none of your w3wiki" ;
Pattern pattern = Pattern.compile( "w3wiki" );
Matcher matcher = pattern.matcher(subject);
int i = 1 ;
while (matcher.find()) {
System.out.println( "\nMatched string is " + matcher.group( 0 ));
System.out.println( "and it is found at position " + matcher.start());
i++;
subject = subject.substring(matcher.end());
matcher = pattern.matcher(subject);
}
}
}
|
Python3
import re
subject = "My w3wiki is my w3wiki none of your w3wiki"
match = re.search( "w3wiki" , subject)
i = 1
while match:
print ( "\nMatched string is" , match.group( 0 ))
print ( "and it is found at position" , match.start())
i + = 1
subject = subject[(match.end()):]
match = re.search( "w3wiki" , subject)
|
C#
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main( string [] args)
{
string subject = "My w3wiki is my w3wiki none of your w3wiki" ;
Regex r = new Regex( "w3wiki" );
Match match;
int i = 1;
while ((match = r.Match(subject)).Success)
{
Console.WriteLine( "\nMatched string is " + match.Value
+ "\nand it is found at position " + match.Index);
i++;
subject = subject.Substring(match.Index + match.Length);
}
}
}
|
Javascript
let subject = "My w3wiki is my w3wiki none of your w3wiki" ;
let match = subject.match( "w3wiki" );
let i = 1;
while (match) {
console.log( "\nMatched string is" , match[0]);
console.log( "and it is found at position" , match.index);
i += 1;
subject = subject.substring(match.index + match[0].length);
match = subject.match( "w3wiki" );
}
|
Output
Matched string is w3wiki
and it is found at position 3
Matched string is w3wiki
and it is found at position 7
Matched string is w3wiki
and it is found at position 14
- Note: Above code is running perfectly fine but the problem is input string will be lost.
- Using iterator: Object can be constructed by calling the constructor with three parameters: a string iterator indicating the starting position of the search, a string iterator indicating the ending position of the search, and the regex object. Construct another iterator object using the default constructor to get an end-of-sequence iterator.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string subject( "w3wikiabcdefghg"
"eeksforBeginnerabcdw3wiki" );
regex re( "Beginner(for)Beginner" );
for (sregex_iterator it = sregex_iterator(subject.begin(), subject.end(), re);
it != sregex_iterator(); it++) {
smatch match;
match = *it;
cout << "\nMatched string is = " << match.str(0)
<< "\nand it is found at position "
<< match.position(0) << endl;
cout << "Capture " << match.str(1)
<< " at position " << match.position(1) << endl;
}
return 0;
}
|
Java
import java.util.regex.*;
public class Main {
public static void main(String[] args)
{
String subject
= "w3wikiabcdefghw3wikiabcdw3wiki" ;
Pattern pattern
= Pattern.compile( "Beginner(for)Beginner" );
Matcher matcher = pattern.matcher(subject);
while (matcher.find()) {
System.out.println(
"\nMatched string is = " + matcher.group( 0 )
+ "\nand it is found at position "
+ matcher.start());
System.out.println( "Capture " + matcher.group( 1 )
+ " at position "
+ matcher.start( 1 ));
}
}
}
|
Python3
import re
subject = "w3wikiabcdefghw3wikiabcdw3wiki"
pattern = r "Beginner(for)Beginner"
matches = re.finditer(pattern, subject)
for match in matches:
print (f "\nMatched string is = {match.group(0)}\nand it is found at position {match.start(0)}" )
print (f "Capture {match.group(1)} at position {match.start(1)}" )
|
C#
using System;
using System.Text.RegularExpressions;
class MainClass {
public static void Main()
{
string subject
= "w3wikiabcdefghw3wikiabcdw3wiki" ;
Regex re = new Regex( "Beginner(for)Beginner" );
foreach (Match match in re.Matches(subject))
{
Console.WriteLine( "\nMatched string is = "
+ match.Value);
Console.WriteLine( "and it is found at position "
+ match.Index);
Console.WriteLine(
"Capture " + match.Groups[1].Value
+ " at position " + match.Groups[1].Index);
}
}
}
|
Javascript
let subject = "w3wikiabcdefghw3wikiabcdw3wiki" ;
let pattern = /Beginner( for )Beginner/g;
let match;
while ((match = pattern.exec(subject)) !== null ) {
console.log( "\nMatched string is = " + match[0]);
console.log( "and it is found at position " + match.index);
console.log(
"Capture " + match[1]
+ " at position " + (match.index + match[0].indexOf(match[1]))
);
}
|
Output
Matched string is = w3wiki
and it is found at position 0
Capture for at position 5
Matched string is = w3wiki
and it is found at position 21
Capture for at position 26
Matched string is = w3wiki
and it is found at position 38
Capture for at position 43
Contact Us