Page 1 of 1

Conditional and Lookbehind

Posted: Sun Jun 08, 2008 5:11 am
by veleshanas
Hello Forum,

I am writing a regex that concatenates several words into one character string. I am using conditional and lookbehind constructions but I keep getting unexpected results. Could anyone help me understand what is going on?

The input for the regex is a few words:
yom sase rare imas ita

The regex should concatenate them with the following rules:
1. The first consonant of a word should be deleted if the word immediately before ends with a consonant. E.g., yom sase —> yomase.
2. The first vowel of a word should be deleted if the word immediately before ends with a vowel. E.g., tabe imasu —> tabemasu.

Since vowels and consonants are mutually exclusive, I thought I could define them as [aeiou] and [^aeiou], and use a regex conditional to express the two rules by one regex. Unfortunately, my regex below matches more than necessary.

For yom sase rare imas ita,

Code: Select all

/(?(?<=\B[^aeiou]\b ))\b[^aeiou]|\b[aeiou]/
matches
[!MATCH!]om[!MATCH!][!MATCH!]ase[!MATCH!][!MATCH!]are[!MATCH!][!MATCH!]mas[!MATCH!][!MATCH!]ta
where the matched portions are replaced by [!MATCH!].

What I want to match in the same convention would be:
yom [!MATCH!]ase rare [!MATCH!]mas ita


THANK YOU in advance for comments!

Re: Conditional and Lookbehind

Posted: Sun Jun 08, 2008 5:31 am
by prometheuzz
Something like this?

Code: Select all

<?php
$test = "yom sase rare imas ita";
echo $test . "\n";
echo preg_replace('/([^aeiou])\s+[^aeiou]|([aeiou])\s+[aeiou]/', '$1$2', $test) . "\n";
/* output:
yom sase rare imas ita
yomase raremas ita
*/
?>

Re: Conditional and Lookbehind

Posted: Tue Jun 10, 2008 10:15 am
by veleshanas
Hello prometheuzz,

It works!! Thank you!

I hope I am not asking too much but how would you write a regex to limit the text input for this concatenation? The input should be:
A. alphabets only OR
B. alphabets and spaces BUT NOT
C. spaces only


Can I use conditional to write something like this?
IF the input is spaces, THEN select nothing, ELSE select alphabets and spaces.

Re: Conditional and Lookbehind

Posted: Tue Jun 10, 2008 10:40 am
by prometheuzz
veleshanas wrote:Hello prometheuzz,

It works!! Thank you!

I hope I am not asking too much but how would you write a regex to limit the text input for this concatenation? The input should be:
A. alphabets only OR
B. alphabets and spaces BUT NOT
C. spaces only


Can I use conditional to write something like this?
IF the input is spaces, THEN select nothing, ELSE select alphabets and spaces.
You're welcome.

I don't know what exactly you mean by "alphabet", but rejecting input strings that consist of entirely white space characters (or empty input strings) can be done like this:

Code: Select all

if(!preg_match('/^\s*$/', $input)) {
  # your code here
}

Re: Conditional and Lookbehind

Posted: Tue Jun 10, 2008 12:39 pm
by GeertDD
prometheuzz wrote:

Code: Select all

if(!preg_match('/^\s*$/', $input)) {
  # your code here
}
For what it is worth, this can be achieved (a bit quicker?) without regular expressions as well.

Code: Select all

if (trim($str) !== '') { /*your code*/ }