RegEx - Conditional Sentences

Hi everyone,

i have the following structure I’d like to parse with RegEx:

[code]<?[Expression]|True>

<?[Expression]|True|False> <?[Expression]||False>[/code] I wrote a pattern, which only returns the second and third line. [code]<\\?\\[(.*)\\]\\|(.*)\\|(.*)?>

$1 returns the Expression
$2 returns the true statement
$2 returns the false statement
[/code]

The first line will not match. Why?

Because your pattern requires 2 pipe characters for a match (the ? only makes the last part optional, e.g. the (.*)

You’ll probably want something like this

<\?\(.*)\+>

Thanks Mark for your input. I only wanna match the bold things, also if they are empty:

<?[[b]Expression[/b]]|[b]True[/b]|[b]False[/b]>

Try this:

<\?\(.*)\+>

[quote=339198:@Mark Walsh]Try this:

<\?\(.*)\+>[/quote]
Nope. Results are

[code]<?[Expression]|True>. $1 returns the “Expression”, $2 returns “True”

<?[Expression]|True|False> $1 returns the "Expression", $2 returns "True|False" <?[Expression]||False> $1 returns the "Expression", $2 returns "|False"[/code]

Sorry, I thought something like this would work:

^<\?\(.*)\+>$

But I’m not getting the expected results. I’m a little rusty (and was never as good as Kem), so you might need to wait for an expert to come along. Or, you could get the entire second expression, and split on the pipe character.

Thanks Mark, ok I’ll look, if someone else can help.

This should do what you need unless “True” and “False” are changed to something that are not entirely word characters.

(?mi-Us)<\\?\\[(.*)\\]\\|(\\w*)(?:\\|)?(\\w*)>

The structure .* should be avoided in cases where you can be specific, or you have to deal with greediness. Try this:

^<\\?\\[([^]\\r\
]+)\\]\\|([^|\\r\
]*)(?:\\|([^>\\r\
]*))?>$

If there is no “false” condition, there will only be two subgroups.

That will be $5 please. (That’s a little inside joke for @Greg O’Lone :slight_smile: )

Dear Jared, thanks for your input. True and False are only variables. So they can be one word ore one ore more sentences! Thats why this solution is not working fine for me.

[quote=339214:@Kem Tekinay]^<\\?\\[([^]\\r\ ]+)\\]\\|([^|\\r\ ]*)(?:\\|([^>\\r\ ]*))?>$[/quote]
Kem, this works pretty well. Thank you so much. There will be some more structures for the conditional sentence function I try to parse. I Think, there will be some more questions within this thread soon :wink:

Look, there will be also as structure with conditional angle brackets.

[code]== Class Properties ==
[P] - “Max Mustermann” (Name)
[D] - “1th March 1980” (Date)
[L] - “New York” (Location)

== Sentence ==
[P] was born< [D]>< [L]>.[/code]

The things in angle brackets should only replaced by the properties, if the properties aren’t empty. Otherwise they should replaced by “”.

[P] was born< [D] with text behind.>< [L]>.

And how to handle with text before/behind the variables?!

@Kem,
what it the structure of an if in regex ?
if I use the shortcut in regexrx, I get : code[/code]
but the “if” must not be inside parenthesis in fact ?
it seems to be code[/code] ?

@Kem Tekinay How to modify your pattern that the following structure will also supported beside the [ and ] brackets?

[code]<?Expression|True>

<?Expression|True|False> <?Expression||False>[/code]
^<\\?\\[?([^]|\\r\
]+)\\]?\\|([^|\\r\
]*)(?:\\|([^>\\r\
]*))?>$

[quote=339235:@Kem Tekinay] ^<\\?\\[?([^]|\\r\ ]+)\\]?\\|([^|\\r\ ]*)(?:\\|([^>\\r\ ]*))?>$ [/quote]
Hi Kem, I found out, that the Pattern is not working correctly.

[code]<?[Expression]|True>

<?[Expression]|True|False> <?[Expression]||False>[/code] The first match (Expression) is [b][Expression[/b], but only [b]Expression[/b] should match.

I just tried this again in RegExRX and cannot reproduce that. The groups contain exactly what’s expected using your example text.

Hi Kem, I found the bug. The Greedy Mode was off. Works fine :wink:

Hi Kem,

there is a third if…then…else structure:

[code]%\[([^>\r
]*);;?\]

%[Expression;True]
%[Expression;True;False]
%[Expression;;False][/code]

Is it possible to combine it with the pattern above?

^<\\?\\[?([^]|\\r\ ]+)\\]?\\|([^|\\r\ ]*)(?:\\|([^>\\r\ ]*))?>$

How to extend to match all structures?

[code]<?Expression|True>

<?Expression|True|False> <?Expression||False> <?[Expression]|True> <?[Expression]|True|False> <?[Expression]||False> %[Expression;True] %[Expression;True;False] %[Expression;;False][/code]