Jump to content

Assigning Values within Regex


Xeoncross

Recommended Posts

Well, we all know the php code for if/else conditional statements.

 

<?php
$var = ($value ? true : false);
?>

 

However, I can't find any data on the regex version. I know I read somewhere you can assign values based on conditions - I just can't find it. This is wrong, but I am thinking of something like:

 

(?([a-zA-Z]+)|[0-9])

 

Where you would could set the output "\\0" to "4' or "somestring".

 

I could always use preg_replace_callback() to check for values - but I want to keep all my checking within the regex. So can anyone help me with this?

Link to comment
Share on other sites

The syntax you are using is also a conditional statement; you can set two ways of matching a string in the same regexp. I.e.

 

<?php 
# if a line starts w/ a number then match something, otherwise something else
#(?(condition)true case|false case)
$rex='/^(?(?=\d+)something|somethingelse)/m';

 

In a way you could set your output, but it is not its first aim. I'd keep on using a callback function to check your values.

Link to comment
Share on other sites

Thanks I couldn't find anything at first because I didn't know what this stuff was called. (Lookahead Assertions). So I have two questions, first when you create an "Assertion" does it count as one of your "\\0" replacement groups or not?

 

Second, I would like to check for a "http://, ftp://" start to a url - and if there isn't one add "http:" to the front of it. (if there is a "[a-z]+://" then don't add anything) Can you look at my code and see what I am doing wrong?

 

<?php
$text = ' http://site.com '. "\n".
           ' site.com ';

$text = preg_replace("/(?(?=[a-zA-Z]+:\/\/)thevalueintheassertion|http:\/\/)/", "\\0", $text);
?>

 

Here are some pages I found on the subject:

http://jregex.sourceforge.net/syntax.html#asserts

http://www.regular-expressions.info/lookaround.html

http://docs.python.org/lib/re-syntax.html

Link to comment
Share on other sites

[...]

Second, I would like to check for a "http://, ftp://" start to a url - and if there isn't one add "http:" to the front of it. (if there is a "[a-z]+://" then don't add anything) Can you look at my code and see what I am doing wrong?

 

Well, you could not hard-set your output within your regular expression. What I've told before is that using a conditional statement you will know in advance that your output will be "A" or "B"  match, but you couldn't force your output to be "A", "B" or some thing else.

You needs to work with a callback function to check & force your output.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.