PHP regular expression with or operation - php

I would like to use a regex over strings like:
1-* or *-2.
This is what I've come up with so far:
"/(.*\-2)||(1\-.*)/"
but it doesn't seem to work - it returns true every time, regardless of my input string.
How can I create a regular expression to match these strings?

Try this (simplistic) version:
/(^1-|-2$)/
If you need to match more specific, add [0-9] at the corresponding positions and maybe another anchor (^$).
/(^1-[0-9]$|^[0-9]-2$)/

Try this ...
"/(.+-\2)|(\1-.+)/"

try this you need add ^ and $ to match the whole string:
/^(1-0|0-2)$/
or:
/^(1-.*|.*-2)$/
select whatever suits your needs

Related

regex and preg_replace_callback

I have a problem with a regular expression.
I'm working with tokens and I have to parse a text like this:
Just some random text
#IT=AB|First statement# #xxxx=xxx|First statement|Second statement#
More text
I use preg_replace_callback since I have to use the first statement or the second one, depending on the first expression is true or not; it's a sort of IF...ELSE... statement.
What I expect are 2 elements like this:
#IT=AB|First statement#
#xxxx=xxx|First statement|Second statement#
So I can start manipulating them inside my callback function.
I tried with this regex /#.*#/, but i get the entire string, it's not parsed into elements.
How can I achieve that? I'm sorry but regex aren't my thing :(
The quantifier * is greedy by default. So a .* will match as much as it can and as a result it'll match a # as well. To fix this you can make the * non-greedy by adding a ? after it. Now a .*? will try to much as little as it can.
/#.*?#/
or you can look for only non # characters between two #:
/#[^#]*#/

regExp problem - string is matched but it should not match

iam trying to check if an user has permission to manage an group:
Expression (ou=|||) is the string I'm looking for
/^OU=|||$|,OU=|||$/i
On a string like "ou=whatever", it returns true (-:
I am sure it's a problem with the pipes, but I have no idea how to solve this.
I am using PHP 5.x with preg_match.
Pipes are metacharacters in a regular expression (meaning "or"). You need to escape them:
/^OU=\|{3}$|,OU=\|{3}$/i
Are you sure that you're using the start- and end-of-string anchors correctly? Right now, this regex will only match the strings
OU=|||
and
<any number of characters>,OU=|||
You need to escape the pipes and include some parenthesis for better readability:
/(^OU=\|\|\|$)|(,OU=\|\|\|$)/i
$has_permission = in_array('OU=|||', explode(',', $permission_string));

PHP string replace question

If I have a string that equals "firstpart".$unknown_var."secondpart", how can I delete everything between "firstpart" and "secondpart" (on a page that does not know the value of $unknown_var)?
Thanks.
Neel
substr_replace
start and length can be computed with strpos. Or you could go the regex route if you're comfortable learning about them.
As long as $unkonwn_var does not contain neither firstpart nor secondpart, you can match against
firstpart(.*)secondpart
and replace it with
firstpartsecondpart
You shoukd use a regexp to do so.
preg_replace('/firspart(.*)secondpart/','firstpartsecondpart',$yourstring);
will replace anything between the first occurence of firstpart and the last of secondpart, if you want to delete multiple time between first and second part you can make the expression ungreedy by replacing (.*) by (.*?) in the expression
preg_replace('/firspart(.*?)secondpart/','firstpartsecondpart',$yourstring);

How can I check if a string EXACTLY matches a regex pattern?

I'm working on a registration script for my client's product sales website.
I'm currently working on a reference ID input area, and I want to make sure that the reference ID is within the correct parameters of the payment method
The Reference ID will look something like this: XXXXX-XXXXX-XXXXX
I'm trying to use this RegEx pattern to match it: /(\w+){5}-(\w+){5}-(\w+){5}/
This matches it perfectly, but it also matches XXXXX-XXXXX-XXXXXXXXXX
Or at least it finds a match in there. I want it to make sure the entire string matches. I'm not too familiar with RegEx
How can I do this?
You need to use start and finish anchors. Alternatively, if you don't need to capture those groups, you can omit the parenthesis.
Also, the +{5} means match more than once exactly 5 times. I believe you didn't want that so I dropped the +.
/^\w{5}-\w{5}-\w{5}\z/
Also, I used \z so your string doesn't match "abcde-12345-edcba\n".
Use ^ and $ to match the start and end of the input string, respectively.
Also note that your use of + was superfluous, as (\w+){5} means "a word character, at least once, times five" which means it can match at least five times. You probably meant (\w){5} (or just \w{5} if you don't need the backreference; I'll assume in my example that you do).
/^(\w){5}-(\w){5}-(\w){5}$/
put the regular expression in between ^ and $ to match the whole string and check if it matches anything
example:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
Try
/^([\w]{5,5})-([\w]{5,5})-([\w]{5,5})$/i
There are several online regex tester out there, I work with this one before I code.
Enclose it in "^" and "$" thus:
/^(\w+){5}-(\w+){5}-(\w+){5}$/
You need ^ to match the start of the string and $ to match the end:
/^\w{5}-\w{5}-\w{5}$/
Note that (\w+){5} is incorrect because that means five repetitions of \w+, but that in turn means "one or more word characters".
/^(\w){5}-(\w){5}-(\w){5}$/
You need to explicitly say that you want the pattern to start at the beginning of the string and end at it's ending.
You can improve it: /^((\w){5}-){2}(\w){5}$/ ; this way, you can easily modify the number of elements your serial number might have.
Use ^ and $ to mark the start and end of the regex string:
/^\w{5}-\w{5}-\w{5}$/
http://www.regular-expressions.info/anchors.html
In preg, \b marks word boundaries. So you could try with something like
/\b(\w+){5}-(\w+){5}-(\w+){5}\b/

How can I replace only the captured elements of a regex?

I'm trying to extract only certain elements of a string using regular expressions and I want to end up with only the captured groups.
For example, I'd like to run something like (is|a) on a string like "This is a test" and be able to return only "is is a". The only way I can partially do it now is if I find the entire beginning and end of the string but don't capture it:
.*?(is|a).*? replaced with $1
However, when I do this, only the characters preceding the final found/captured group are eliminated--everything after the last found group remains.
is is a test.
How can I isolate and replace only the captured strings (so that I end up with "is is a"), in both PHP and Perl?
Thanks!
Edit:
I see now that it's better to use m// rather than s///, but how can I apply that to PHP's preg_match? In my real regex I have several captured group, resulting in $1, $2, $3 etc -- preg_match only deals with one captured group, right?
If all you want are the matches, the there is no need for the s/// operator. You should use m//. You might want to expand on your explanation a little if the example below does not meet your needs:
#!/usr/bin/perl
use strict;
use warnings;
my $text = 'This is a test';
my #matches = ( $text =~ /(is|a)/g );
print "#matches\n";
__END__
C:\Temp> t.pl
is is a
EDIT: For PHP, you should use preg_match_all and specify an array to hold the match results as shown in the documentation.
You can't replace only captures. s/// always replaces everything included in the match. You need to either capture the additional items and include them in the replacement or use assertions to require things that aren't included in the match.
That said, I don't think that's what you're really asking. Is Sinan's answer what you're after?
You put everything into captures and then replaces only the ones you want.
(.*?)(is|a)(.*?)

Categories