I am using php and I am using regular expressions to try to detect a quotation.
I would like for it to find matches only if it finds a quotation in the string
HELLO
Shouldnt find any matches
"HELLO"
Should find a match
"HELLO
Still should find a match
The regular expression is just the quote character.
echo preg_match('/"/', $string);
That will return 0 for HELLO, 1 for "HELLO" and 1 for "HELLO
You could use a regular expression with preg_match, which checks for any double-quote in $thestring:
if(preg_match('"', $thestring)) { ... }
But it may be faster to use the strpos function instead:
if(strpos($thestring, '"') !== false) { ... }
The code inside the if statements will only be executed if a double-quote is found. My code examples won't return which text was quoted, although you can certainly do that with a few changes.
Use "(.*?)(\n|\r|$|").
Example:
http://img221.imageshack.us/img221/2994/screenshot2z0.png
EDIT: from your comments I understand that you don't need this; what you need is Colin O'Dell's answer (namely the strpos function). Still, I'm leaving this here in case anyone else needs it.
Not so difficult, IMHO :)
I guess you are just trying to determine whether there is a quote in the given string or not, right? So then it can by done by
preg_match('/"/',$string)
or more efficiently by
strpos($string,'"') || $string == '"'
["]*HELLO["]*
will match HELLO with or without quotes on either side.
Or you can use
"{0,1}HELLO"{0,1}
Use (trim($string, '"') !== $string)
Or (trim($string, '"\'') !== $string) for quotation or single quote
Related
I am trying to match . \ or / using preg_match in PHP.
I thought this would do it but it's matching all strings.
$string = '';
$chars = '/(\.|\\|\/)/';
if (preg_match($chars, $string) != 0) {
echo 'Chars found.';
}
Argument given to preg_match() is string. Strings are automatically escaped by PHP. For example, if you have {\\\\} (backslash) given to the regexp engine, PHP will first parse it creating {\\} (\\ is replaced by \).
Next, regexp engine parses the regexp. It sees {\\} which PHP gave to regexp engine. It sees \ as escape character, so it actually matches \ character which was escaped by \.
In your case, it looks like /(\.|\\|\/)/. PHP gives to regexp engine /(\.|\|\/)/ which is actually either . or |/ (notice that | character was escaped).
Personally, I try to avoid escaping meta-characters, especially with how regexp engine works. I usually use [.] instead, it's more readable. Your regexp written with this would look like /([.]|\\\\|[/])/.
It's possible to do few optimizations. While it's my personal thing, I prefer to use {} as delimiters (yes, you can use pairs of characters). Also, your regexp matches single characters, so you could easily write it as {[.\\\\/]}, which is very readable in my opinion (notice four slashes, it's needed because both PHP and regexp engine parse backslashes).
Also, preg_match() returns number of matches. It will be always bigger than 0, so you can easily consider it to be boolean and avoid writting == 0. Instead, you can insert ! before string to make it negative. But I think you accidentally reversed condition (it matches if it doesn't match). Valid code below:
$string = '';
$chars = '{[.\\\\/]}';
if (preg_match($chars, $string)) {
echo 'Chars found.';
}
Your if logic is flawed. preg_match will return the number of matches. Therefore, == 0 means "no matches".
That said, single quoted strings don't expand escape sequences except \' and \\. You need to double your backslash escape for it to appear in the regex as expected. Change your code to:
$string = '';
$chars = '/(\.|\\\\|\/)/';
if (preg_match($chars, $string) != 0) {
echo 'Chars found.';
}
Here's a test case:
$strings = array('', '.', '/', '\\', 'abc');
$pattern= '/(\.|\\\\|\/)/'
foreach($strings as $string) {
if (preg_match($pattern, $string) > 0) {
printf('String "%s" matched!', $string);
}
}
The issue is probably with PHP. When escaping something in a regex string, you also need to escape the backslashes you use to escape, or PHP will attempt to interpret it as a special character.
As that probably didn't make sense, have an example.
$string = "\." will make PHP attempt to escape the ., and fail. You instead need to change this to $string = "\\\.".
When trying to REGEX match slashes, I would strongly suggest using a different separator character than '/'. It reduces the amount of escaping you need to do and makes it much more readable:
$chars = '%(\.|\\|/)%';
Try this:
$chars = '%(\.|\\\\|/)%'
is there a way to find a word in a sentence using PHP? We have a form and in the subject line we want to redirect someone if they use the words delivery, deliver, or delivered. Can this be done and how would it be? thank you for any help on this subject.
This should do it:
if ( preg_match('/deliver(?:y|ed)?/i', $subject) )
{
// Redirect
}
You can use the strpos function to search for a string inside another string. If the string is not found false will be returned, and you know that your string was not found.
another method:
if (stristr($sentence,"deliver")) {
header('location: somepage.php');
}
But I would use preg_match as expressed before.
One method of many:
if (preg_match('/deliver(y|ed)?/', $string)) {
// yes, $string contained 'deliver', 'delivery' or 'delivered'
}
Here:
<?php
if (strpos($string, "deliver")) {
header("Location: somepage.php");
}
?>
to extract all words, simple explode the string using a whitespace characters
to check a particular word either use the simple strpos() function or a regular expression pattern matching
preg_match("/(?:^|\s+)deliver(y|ed)?(?:$|\s+)/i")
The above expression checks whitespace character or beginning of string, similary whitespace character or end of string
I want to match matching tags like <tag>...</tag>. I tried the regex
~<([^>]+)>.*?</\1>~
but this fails. The expression worked when I used the exact text inside the angle brackets, i.e,
~<(tag)>.*?</tag>~
works, but even
~<(tag)>.*?</\1>~
fails.
I'm assuming that the back reference is not working here.
Can someone help me out please. Thanks
PS: I'm not using this to parse HTML. I know I shouldn't.
You didn't show your PHP code, but I surmise you have your regex in double quotes. If so then the backreference \1 actually is converted into an ASCII character ☺ before it reaches PCRE. (All \123 sequences are interpreted as C-string octal escapes there.)
It worked for me...
$str = '<a></a>';
var_dump(preg_match('~<([^>]+)>.*?</\1>~', $str)); // int(1)
CodePad.
Also, have you considered an XML parser? Otherwise it won't like a piece of HTML like this...
<a title="Is 4 > 6?"></a>
CodePad.
Apart from the fact that it's not always a good idea to try and match markup languages using regex, your regex looks OK. Maybe you're using it wrong?
if (preg_match('~<([^>]+)>.*?</\1>~', $subject, $regs)) {
$result = $regs[0];
} else {
$result = "";
}
should work.
Use single quotes in the pattern
preg_match_all('/(sens|respons)e and \1ibility/', "sense and sensibility", $matches);
print_r($matches);
i am looking for a regex that can contain special chracters like / \ . ' "
in short i would like a regex that can match the following:
may contain lowercase
may contain uppercase
may contain a number
may contain space
may contain / \ . ' "
i am making a php script to check if a certain string have the above or not, like a validation check.
The regular expression you are looking for is
^[a-z A-Z0-9\/\\.'"]+$
Remember if you are using PHP you need to use \ to escape the backslashes and the quotation mark you use to encapsulate the string.
In PHP using preg_match it should look like this:
preg_match("/^[a-z A-Z0-9\\/\\\\.'\"]+$/",$value);
This is a good place to find the regular expressions you might want to use.
http://regexpal.com/
You can always escape them by appending a \ in front of the special characters.
try this:
preg_match("/[A-Za-z0-9\/\\.'\"]/", ...)
NikoRoberts is 100% correct.
I would only add the following suggestion: When creating a PHP regex pattern string, always use: single-quotes. There are far fewer chars which need to be escaped (i.e. only the single quote and the backslash itself needs to be escaped (and the backslash only needs to be escaped if it appears at the end of the string)).
When dealing with backslash soup, it helps to print out the (interpreted) regex string. This shows you exactly what is being presented to the regex engine.
Also, a "number" might have an optional sign? Yes? Here is my solution (in the form of a tested script):
<?php // test.php 20110311_1400
$data_good = 'abcdefghijklmnopqrstuvwxyzABCDE'.
'FGHIJKLMNOPQRSTUVWXYZ0123456789+- /\\.\'"';
$data_bad = 'abcABC012~!###$%^&*()';
$re = '%^[a-zA-Z0-9+\- /\\\\.\'"]*$%';
echo($re ."\n");
if (preg_match($re, $data_good)) {
echo("CORRECT: Good data matches.\n");
} else {
echo("ERROR! Good data does NOT match.\n");
}
if (preg_match($re, $data_bad)) {
echo("ERROR! Bad data matches.\n");
} else {
echo("CORRECT: Bad data does NOT match.\n");
}
?>
The following regex will match a single character that fits the description you gave:
[a-zA-Z0-9\ \\\/\.\'\"]
If your point is to insure that ONLY characters in this range of characters are used in your string, then you can use the negation of this which would be:
[^a-zA-Z0-9\ \\\/\.\'\"]
In the second case, you could use your regex to find the bad stuff (that you don't want to be included), and if it didn't find anything then your string pattern must be kosher, because I'm assuming that if you find one character that is not in the proper range, then your string is not valid.
so to put it in PHP syntax:
$regex = "[^a-zA-Z0-9\ \\\/\.\'\"]"
if preg_match( $regex, ... ) {
// handle the bad stuff
}
Edit 1:
I've completely ignored the fact that backslashes are special in php double-quoted strings, so here is a correcting to the above code:
$regex = "[^a-zA-Z0-9\\ \\\\\\/\\.\\'\\\"]"
If that doesn't work it shouldn't take too much for someone to debug how many of the backslashes need to be escaped with a backslash, and what other characters need also to be escaped....
I figured out how to check an OR case, preg_match( "/(word1|word2|word3)/i", $string );. What I can't figure out is how to match an AND case. I want to check that the string contains ALL the terms (case-insensitive).
It's possible to do an AND match in a single regex using lookahead, eg.:
preg_match('/^(?=.*word1)(?=.*word2)(?=.*word3)/i', $string)
however, it's probably clearer and maybe faster to just do it outside regex:
preg_match('/word1/i', $string) && preg_match('/word2/i', $string) && preg_match('/word3/i', $string)
or, if your target strings are as simple as word1:
stripos($string, 'word1')!==FALSE && stripos($string, 'word2')!==FALSE && stripos($string, 'word3')!==FALSE
I am thinking about a situation in your question that may cause some problem using and case:
this is the situation
words = "abcd","cdef","efgh"
does have to match in the string:
string = "abcdefgh"
maybe you should not using REG.EXP
If you know the order that the terms will appear in, you could use something like the following:
preg_match("/(word1).*(word2).*(word3)/i", $string);
If the order of terms isn't defined, you will probably be best using 3 separate expressions and checking that they all matched. A single expression is possible but likely complicated.
preg_match( "/word1.*word2.*word3)/i");
This works but they must appear in the stated order, you could of course alternate preg_match("/(word1.*word2.*word3|word1.*word3.*word2|word2.*word3.*word1|
word2.*word1.*word3|word3.*word2.*word1|word3.*word1.*word2)/i");
But thats pretty herendous and you'd have to be crazy!, would be nicer to just use strpos($haystack,$needle); in a loop, or multiple regex matches.