Preg_match when string is sometimes a single word? - php

I'm trying to pull a word out of an email subject line to use as a category for attached email. Preg_match works great as long as it's not just a single word (which is what I'd like to do anyway). If there is only one word in the subject line, I just get an empty array. I've tried to treat $matches as just a variable in that case, but that doesn't work either. Can anyone tell me if preg_match will work on a single word, or what the better way to do this would be?
Thanks very much

Assuming \b(?:word1|word2|word3)\b
The reason it wont match "word1" is because you included a word separator, the \b.
What you can do is just simply always inject the word separator:
preg_match("\b(?:word1|word2|word3)\b", "." . $subject . ".", $matches);
Crude but effective.

preg_match will work on a string one character long. I think that the issue here is probably your regex. My guess is that you're testing for whitespace and because it isn't finding any it says that there is no match. Try appending '^([^\s]*)$|' to your regex and I wager it will start picking up those one word values. ([^\s] means give me anything which has no spaces in it, | means 'or'. By adding it to the front of your regex, it will include things without whitespace or whatever you already had)

Related

Building a regex expression for PHP

I am stuck trying to create a regex that will allow for letters, numbers, and the following chars: _ - ! ? . ,
Here is what I have so far:
/^[-\'a-zA-Z0-9_!\?,.\s]+$/ //not escaping the ?
and this version too:
/^[-\'a-zA-Z0-9_!\?,.\s]+$/ //attempting to escape the ?
Neither of these seem to be able to match the following:
"Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?"
Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.
Thanks!
UPDATE:
Thanks to Lix's advice, this is what I have so far:
/^[-\'a-zA-Z0-9_!\?,\.\s]+$/
However, it's still not working??
UPDATE2
Ok, based on input this is what's happening.
User inputs string, then I run the string through following functions:
$comment = preg_replace('/\s+/', '',
htmlspecialchars(strip_tags(trim($user_comment_orig))));
So in the end, user input is just a long string of chars without any spaces. Then that string of chars is run using:
preg_match("#^[-_!?.,a-zA-Z0-9]+$#",$comment)
What could possibly be causing trouble here?
FINAL UPDATE:
Ended up using this regex:
"#[-'A-Z0-9_?!,.]+#i"
Thanks all! lol, ya'll are going to kill me once you find out where my mistake was!
Ok, so I had this piece of code:
if(!preg_match($pattern,$comment) || strlen($comment) < 2 || strlen($comment) > 60){
GEEZ!!! I never bothered to look at the strlen part of the code. Of course it was going to fail every time...I only allowed 60 chars!!!!
When in doubt, it's always safe to escape non alphanumeric characters in a class for matching, so the following is fine:
/^[\-\'a-zA-Z0-9\_\!\?\,\.\s]+$/
When run through a regular expression tester, this finds a match with your target just fine, so I would suggest you may have a problem elsewhere if that doesn't take care of everything.
I assume you're not including the quotes you used around the target when actually trying for a match? Since you didn't build double quote matching in...
Can somebody point out what I am doing wrong? I must point out that my script takes the user input (the paragraph in quotes in this case) and strips all white space so actual input has no white space.
in which case you don't need the \s if it's working correctly.
I got the following code to work as expected to (running php5):
<?php
$pattern = "#[-'A-Z0-9_?!,.\s]+#i";
$string = "Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?";
$results = array();
preg_match($pattern, $string, $results);
echo '<pre>';
print_r($results);
echo '</pre>';
?>
The output from print_r($results) was as following:
Array
(
[0] => Oh why, oh why is this regex not working! It's getting pretty frustrating? Frustrating - that is to say the least. Hey look, an underscore_ I wonder if it will match this time around?
)
Tested on http://writecodeonline.com/php/.
It's not necessary to escape most characters inside []. However, \s will not do what you want inside the expression. You have two options: either manually expand (/^[-\'a-zA-Z0-9_!?,. \t\n\r]+$/) or use alternation (/^(?:[-\'a-zA-Z0-9_!?,.]|\s)+$/).
Note that I left the \ before the ' because I'm assuming you're putting this in a PHP string and I wouldn't want to suggest a syntax error.
The only characters with a special meaning within a character class are:
the dash (since it can be used as a delimiter for ranges), except if it is used at the beginning (since in this case it is no part of any range),
the closing bracket,
the backslash.
In "pure regex parlance", your character class can be written as:
[-_!?.,a-zA-Z0-9\s]
Now, you need to escape whatever needs to be escaped according to your language and how strings are written. Given that this is PHP, you can take the above sample as is. Note that \s is interpreted in character classes as well, so this will match anything which is matched by \s outside of a character class.
While some manuals recommend using escapes for safety, knowing the general regex rules for character classes and applying them leads to shorter and easier to read results ;)

preg_replace proper regex for repeating character

OK I'm stuck again, this time it's a problem with the regex... Was searching google, was searching SO, but there wasn't a post that made me happy... So to make a long story short:
§text = Database entry string -> could be everything
$text gets parsed and the regex should replace everything between 2 * with:
[bla].$matchedtext.[blub]
So I've tried to find the right regex for that and that's what I came up with:
$text= preg_replace('~(/\*([^\"]*?)\*/)~', "$1<b>$2</b>", $text);
And the 2 * per match should disappear as well :/...
Obviously it doesn't work, elsewhise I wouldn't post :D -> Any ideas?
This should probably do it:
preg_replace('/\*([^"*]*)\*/', '<b>\1</b>', $text);
A few comments on your earlier regular expression:
[^\"]*?
The non-greedy * is not necessary; when you're looking at a negative character set, simply add the '*' inside the character set. Also, the double quote doesn't need escaping.
[^"*]*
You only need memory groups for things you wish to remember; in your case, you don't want to know that you matched a beginning and ending asterisk. So you can do your whole matching with just one memory group.

till the end of the string - Regex

I'm parsing an external feed which contains location and date inside post title which I want to get rid of, so:
This happened on Date in Location
I need to find on (space on space) and remove everything till the end of the line, same for in(space in space).
I googled a bit, but regex is really unfathomable for me so I'd appreciate any help.
Thanks!
Well, a literal "on" does match exactly. Then tell the regex engine to match everything after: ".*". (Note, that the . doesn't match newlines, so it works as needed.)
In the case of "in" you need an alternative, which is marked by parentheses () and the vertical bar |: "(on|in)". You could also make that a bit tighter with character classes []: "[oi]n".
With that we arrive at this regex:
/ [oi]n .*/
To the end of the line? Then I suppose:
preg_replace("/(?:on|in).*?(\n|$)/", "", 'This happened on Date in Location');
Would do it.
Use a negative lookbehind if you want to remove everything after the on and in but not the on and in themselves.
(?<=\son\s).*
and
(?<=\sin\s).*
http://regexr.com?30ops

Need php regex between 2 sets of chars

I need a regular expression for php that outputs everything between <!--:en--> and <!--:-->.
So for <!--:en-->STRING<!--:--> it would output just STRING.
EDIT: oh and the following <!--:--> nedds to be the first one after <!--:en--> becouse there are more in the text..
The one you want is actually not too complicated:
/<!--:en-->(.*?)<!--:-->/gi
Your matches will be in capture group 1.
Explanation:
The .*? is a lazy quantifier. Basically, it means "keep matching until you find the shortest string that will still fit this pattern." This is what will cause the matching to stop at the first instance of <!--:-->, rather than sucking up everything until the last <!--:--> in the document.
Usage is something like preg_match("/<!--:en-->(.*?)<!--:-->/gi", $input) if I recall my PHP correctly.
If you have just that input
$input = '<!--:en-->STRING<!--:-->';
You can try with
$output = strip_tags($input);
Try:
^< !--:en-- >(.*)< !--:-- >$
I don't think any of the other characters need to be escaped.
<!--:en--\b[^>]*>(.*?)<!--:-->
This will match the things between your tags. This will break if you nest your tags, but you didnt say you were doing that :)

Match a regular expression against any non-character or number

Ok, here again.
I'll promise to study deeply the regular expression soon :P
Language: PhP
Problem:
Match if some badword exist inside a string and do something.
The word must be not included inside a "greater word". I mean if i'll search for "rob" (sorry Rob, i'm not thinking you're a badword), the word "problem have to pass without check.
I'd googled around but found nothing good for me. So, I thought something like this:
If i match the word with after and before any character of the following:
.
,
;
:
!
?
(
)
+
-
[whitespace]
I can simulate a check against single word inside a string.
Finally the Questions:
There's a better way to do it?
If not, which will be the correct regexp to consider [all_that_char]word[all_that_char]?
Thanks in advance to anyone would help!
Maybe this is a very stupid question but today is one of that day when move our neurons causes an incredible headache :|
Look up \b (word boundary):
Matches at the position between a word
character (anything matched by \w) and
a non-word character (anything matched
by [^\w] or \W) as well as at the
start and/or end of the string if the
first and/or last characters in the
string are word characters.
(http://www.regular-expressions.info/reference.html)
So: \brob\b matches rob, but not problem.
You can use \b, see Whole word bounderies.

Categories