Regular Expression Matching all permutations [closed] - regex

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to find all the permutations of 0123 in the string below
01210210021212333212300213231102023103130001332121230221000012333333021032112
can i have a regular expression that can give me the permutations of 0123 matching in the string ?
Also i need if there are any overlapped patters
"0123" here i want a match of [1023][1230][2301][3012]

Not regex, but C++11:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
const std::string s("01210210021212333212300213231102023103130001332121230221000012333333021032112");
const std::string ref("0123");
if(ref.length() > s.length())
{
return 0;
}
for(int i = 0; i < s.length() - ref.length(); ++i)
{
if(std::is_permutation(s.cbegin()+i, s.cbegin()+i+ref.length(), ref.cbegin()))
{
const std::string extract(s, i, ref.length());
std::cout << extract << std::endl;
}
}
return 0;
}
To be compiled for example with g++ -std=c++11 -o sample sample.cpp
If you absolutely need regex: (?=[0123]{3})(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3). which means:
(?=[0123]{3}) : positive assertion that the 4 next characters are 0, 1, 2, 3
(.) : capture first character
(?!\1) : assert that following character is not the first capture group
(.) : capture second character
(?!\1|\2) : assert that following character is neither the first nor the second capture group
etc.

A regular expression cannot do what you're asking for. It cannot generate permutations from a string.

Related

Given a string output it in a specific way using recursion? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}

Finding matching letters in a string at same spots [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need a code to compare two strings: word and ptw. Both strings only have one word, and are of equal length and same case. The code needs to check the first character of both words and see if they are same and it will do this untill the end of the word. Then it will output the amount of matching letters (I don't need to know the matching letters).
Example: If word is windows and ptw is winowes it should output 4 as w, i, n, and s match.
I have tried the following however it does not check the positions:
string matchingletters(string word, string ptw) {
string result = "";
sort(begin(word), end(word));
sort(begin(ptw), end(ptw));
std::string intersection;
std::set_intersection(begin(word), end(word), begin(ptw), end(ptw),
back_inserter(intersection));
string mlr = to_string(intersection.length());
result = mlr + result;
cout << result << endl;
return result;
}
The result this gives when word is kanton and ptw is balkon is 4.
It counts k even though k is at 0 position in word and 3 position at ptw and thus they are not in the same postion and should not be counted.
Assuming the two words have the same length and since you don't care which letters match you can simply iterate and count matching characters
unsigned matchingletters(const std::string& word, const std::string& ptw) {
assert(word.size() == ptw.size());
unsigned count{0};
for (size_t i = 0; i < word.size(); ++i) {
if(word[i] == ptw[i])
count++;
}
return count;
}

Could someone help me with Regex? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I need to check if all the "B" to the right and the "A" to the left. No matter the position of the "#", as long as all "B" are all on one side and "A" on the other. For example:
-Correct-
BBAA#
BBA#A
BBBB#AAAA
B#BBAAA
#BBAA
-Incorrect-
AAA#BBB
AAB#ABB
B#AAB
BAA#B
#ABAB
BABA#
ABBB#AA
Someone could help me .... I already tried but I could not
thanks
code:
#include <iostream>
#include<regex>
using namespace std;
auto check = [](string &rp)->bool {
regex reg("^(B)*(A)*");
return regex_match(rp, reg);
};
int main()
{
string rp;
do{
system("cls");
cout<<"RP: ";
getline(cin, rp);
}while (!check(rp));
cout<<"\n valid"<< rp<<endl;
return 0;
}
Regex won't move the text for you it will just tell you if the pattern is correct or not. You're going to need another tool alongside regex.
Edit: this regex should work for you. It matches 0 or more #s followed by zero or more Bs 0 or more times followed by 0 or more #s followed by 0 or more As zero or more times. ^ and $ denote the beginning and end of the string.
^(#*B*)*(#*A*)*$

Get the first character of a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
The title pretty much says it all. I would like to know how to convert a one character string to a char.
Any help is appreciated!
That's the relatively simple:
char ch = str[0];
In other words, just grab the first character from the string, assuming it's not empty.
There's plenty of other stuff you could do, like handling an empty string or skipping leading whitespace, but the code above should be fine for your specific question.
You can do this using the subscript operator on the string, example:
string a = "hello";
char b;
if (!a.empty())
b = a[0];
std::string is a container of contiguous characters providing random access to their elements. There are at least three straight forward ways to retrieve the first character in a string
#include <string>
...
std::string string{ "Hello" };
char c1{ string[ 0 ] }, // undefined when called on an empty string
c2{ string.at( 0 ) }, // will throw if used on an empty string
c3{ string.front() }; // C++11 equivalent to string[ 0 ]
...
String is actually sequence of characters. And you can get any character you want from that sequence.
For example:
if you have string hello, world that is just sequence of characters:
h e l l o , w o r l d. Where index of first character h is 0 and index of last character is 11.
Same rules apply to one character string:
#include <cstdio>
int main() {
char text[] = "h";
printf("%s\n", text);
char first = text[0];
printf("%c\n", first);
return 0;
}
Here you have string h which is sequence of characters containing only one character. :D
Index of character h in that string is 0 so you can get that character with text[0].

How to find string that contain dash in a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I try to get the position of the string "-a" with this code
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string command("test –a string");
size_t pos = command.find("-a");
cout << "position found = " << pos << endl;
}
this produce this output:
position found = 4294967295
If I remove the '-' it's work as expected. 8 is returned.
You get the string::npos value returned, because the library thinks that it cannot find -a in the string.
The reason for this is that you use different dashes a long dash – in the string and a short dash - in the search string.
Once you replace the character with the correct one in both places, your code starts working fine (demo).
It means that there are different the first characters.
You can check this using the first characters and placing them in statement
std::cout << ( '–' == '-' ) << std::endl;
As they are different function find returns value std::string::npos that is defined as std::string::size_type( -1 ) or equal to 4294967295
If you look really close you will find the '–' is no '-'.