1

Well, I've got a propositional logic sentence like this :

~(((-P|-Q)->(P->Q))&((P->Q)->(-P|Q)))  

and what I'd like to do, it's to add spaces between each character; and get something like this:

~ ( ( ( -P | -Q ) -> ( P -> Q ) ) & ( ( P -> Q ) -> ( -P | Q ) ) )  

Moreover; I wouldn't like to add spaces only in those joined character -> and -P because represent an operand and a negative statements. I had found a regular expression which added spaces, but it did it with all the characters, even with those which shouldn't have. this is the expression i had found:

(?<=.)(?!$) 

So; any help for doing it; doesn't matter whether is either a method or the same regular expression but modified.

4
  • 1
    Can you add spaces between every character? Can you remove spaces that follow a "-"? If you can do both those things, you can solve your problem. Commented Dec 18, 2016 at 1:08
  • thinking about it; i hadn't thought it. Your'e right, i'll try. Commented Dec 18, 2016 at 1:11
  • but is there a way of doing it with the regular expression i wrote? because that would be easier rather than creating method. Commented Dec 18, 2016 at 1:14
  • @superkiller170793 There is nothing complicated or difficult about creating methods, and regular expressions are notorious for making simple tasks way more complicated than they need to be. If you can write the method, you should do that. Commented Dec 18, 2016 at 1:18

4 Answers 4

2

Version simple to understand:

string initial = "~(((-P|-Q)->(P->Q))&((P->Q)->(-P|Q)))";

string result = string.Join<char> (" ", initial).Replace ("- ", "-");
Sign up to request clarification or add additional context in comments.

Comments

1
string subject = "~(((-P|-Q)->(P->Q))&((P->Q)->(-P|Q)))",
       pattern = @"([\(\)|QP~&]|-P|-Q|->)";

Regex reg = new Regex(pattern);

Console.WriteLine(reg.Replace(subject, "$1 "));

Above code outputs:

~ ( ( ( -P | -Q ) -> ( P -> Q ) ) & ( ( P -> Q ) -> ( -P | Q ) ) )

Regex Pattern Explained:

https://regex101.com/r/b7STUP/2

Comments

0

You can use Linq, first Aggregate the string by adding spaces then use Replace to remove the space on the right of each hyphen.

string initial = "~(((-P|-Q)->(P->Q))&((P->Q)->(-P|Q)))";
string result = initial.Aggregate("", (c, i) => c + i + ' ').Replace(" - ", " -").Trim();

Comments

0

Simple thought: -Make a char[] from it -Loop through it to a list adding a space every % 2 -Join the list to a string -Trow a replace over it to convert - > to -> and such

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.