regular expressions checking two strings - php

Hi wonder if anyone can help - I'm trying to check for occurance of one of two possible strings using regex - but my knowlege of regex is very limited, so I'm not having much sucess.
I'm trying to look for 'Email' and 'eMailConfirm', this is what I have so far and is working for Email
subject is the id of a input field, so it could be 'name','Email','eMailConfirm'
$subject = $getPromoOuter['label'];
$pattern = '/^Email/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 0);
I tried a number of potential expressions to try and incorporate the second string but I can't seem to get it to play (plus a few guesswork ones based on others)
any idea how I can concatenate those two strings and check for an occurance of either?
Thanks for looking

I'll just place an answer here, as I do think I have a good idea what your requirement is.
Your current regex is /^Email/ which matches any string which starts with 'Email'. (whether or not it has to start with it is unclear to me).
In case you need to match either Email or eMailConfirm, not at the start of the string, you should go for
/Email|eMailConfirm/
If the matches do need to be at the front of the string, just prepend both with a '^' character:/^Email|^eMailConfirm/

Related

Correct regex for this pattern

I've got some issues understanding this regex.
I tried doing a pattern but does not work like intended.
What I want is [A-Za-z]{2,3}[0-9]{2,30}
That is 2-3 letters in the beginning and 2-30 numbers after that
FA1321321
BFA18098097
I want to use it to validate an input field but can't figure out how the regex should look like.
Can any one that can help me out even explain a bit about it?
Your regex is correct - just make sure to surround it with / in PHP, and perhaps ^, $ if you want it to strictly match the entire string (no extra characters before/after).
$pattern = "/^[A-Za-z]{2,3}[0-9]{2,30}$/"
$found = preg_match($pattern, $your_str);
From the PHP documentation:
preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

How to get a number from a html source page?

I'm trying to retrieve the followed by count on my instagram page. I can't seem to get the Regex right and would very much appreciate some help.
Here's what I'm looking for:
y":{"count":
That's the beginning of the string, and I want the 4 numbers after that.
$string = preg_replace("{y"\"count":([0-9]+)\}","",$code);
Someone suggested this ^ but I can't get the formatting right...
You haven't posted your strings so it is a guess to what the regex should be... so I'll answer on why your codes fail.
preg_replace('"followed_by":{"count":\d')
This is very far from the correct preg_replace usage. You need to give it the replacement string and the string to search on. See http://php.net/manual/en/function.preg-replace.php
Your second usage:
$string = preg_replace(/^y":{"count[0-9]/","",$code);
Is closer but preg_replace is global so this is searching your whole file (or it would if not for the anchor) and will replace the found value with nothing. What your really want (I think) is to use preg_match.
$string = preg_match('/y":\{"count(\d{4})/"', $code, $match);
$counted = $match[1];
This presumes your regex was kind of correct already.
Per your update:
Demo: https://regex101.com/r/aR2iU2/1
$code = 'y":{"count:1234';
$string = preg_match('/y":\{"count:(\d{4})/', $code, $match);
$counted = $match[1];
echo $counted;
PHP Demo: https://eval.in/489436
I removed the ^ which requires the regex starts at the start of your string, escaped the { and made the\d be 4 characters long. The () is a capture group and stores whatever is found inside of it, in this case the 4 numbers.
Also if this isn't just for learning you should be prepared for this to stop working at some point as the service provider may change the format. The API is a safer route to go.
This regexp should capture value you're looking for in the first group:
\{"count":([0-9]+)\}
Use it with preg_match_all function to easily capture what you want into array (you're using preg_replace which isn't for retrieving data but for... well replacing it).
Your regexp isn't working because you didn't escaped curly brackets. And also you didn't put count quantifier (plus sign in my example) so it would only capture first digit anyway.

Capturing a pattern of unknown repitition in PCRE

This may be a quick question for experienced regular expressionists, but I'm having trouble getting my match to execute correctly.
Suppose I had a string that looked like this:
http://aaa-bbbb-cc-ddddd-eee-.sub.dom
I would like to go capture all of the "aaa", "bbbb", "cc", and "ddddd" substrings, but I'm not sure how many there will be (e.g., having all triplets up through "zzz").
This is the regular expression I'm trying to use right now:
/http:\/\/(\w*?\-)+\.sub\.dom/
I wrote it this way because:
I want to match substrings, but I want each to terminate when a - is parsed
I want to capture one or more of these substrings
But it seems to only be saving the last match that it makes (in the above case, it would only match "eee-".
Is there a good way to capture all of the matched substrings?
More information: I'm using PHP's PCRE function preg_replace_callback. Thanks!
No, it is not possible to match an unknown number of capture groups.
If you try to repeat a capture group, it will always contain the last value captured.
Could you explain a bit more broadly what you're trying to do? Perhaps there is another simple way to do it (possibly without regular expressions).
If you want the items in the subdomain, and then all matches between the dashes... This should work:
$string = "http://aaa-bbbb-cc-ddddd-eee-.sub.dom";
preg_match("/^http:\/\/([\w-]+?)\..*$/i", $string, $match);
$parts = explode('-', $match[1]);
print_r($parts);
Short of that you will probably have to build a small parsing script to parse the string yourself if that doesn't do it for you.

Matching html input values with regex

I am trying to match a string like the following:
<input type="text" value="cbyEOS56RK3lOxtiCrhmWSkDuNWwrFN4" name="iden">
This is my code:
$pattern = '~value="(.+?)" name="iden"~';
preg_match($pattern, $page, $match);
print_r($match);
As you can probably see, I am trying to match the value in this HTML input. By what I know of regular expressions, .* will match as many characters as possible until it satisfies the next token (in this case ").
I have the name="iden" part in my regex because there are other HTML inputs on the page and I only want to match this one.
Problem is, I'm not getting any matches at all. $match is an empty array. And I know that $page has the right content because I can see it when I echo it.
Help fixing my regex is appreciated, thanks.
Since you used the phrase "there are other inputs on the page", I assume you're trying to parse out this particular tag from a full HTML document. In that case, I recommend using a DOM parser rather than regular expressions (I'm not trying to be facetious with that link, there's just a lot of options so that seemed easiest). They are designed specifically for this purpose and will be a lot easier in the end.
If you want to try regex anyway, I would personally use ([^"]+) instead of (.+?):
$pattern = '~value="([^"]+)" name="iden"~';
Though this still doesn't address whatever is causing your problem, as your regex should match on that line.

PHP Preg (regex) extracting last value from a string? not as simple as I thought

I'm trying to use PHP preg to extract the integer at the end of the string below, in the example its "4", but could be any number coming after "INBOX/", also the "testtester1010#mydomain.com" is a variable so it could be any address
STRING I'M EXTRACTING FROM:
/m/testtester1010#mydomain.com/folder/INBOX/4
/STRING
I've been going in circles with this, I guess I don't really understand how to do this and the regex examples I am finding in my searches just don't seem to address something like this, I would greatly appreciate any help..
ps. If anyone knows of a good regex software for building queries like this (for extraction) I would appreciate letting me know as I have spent countless hours lately with regex and still haven't found a software that seems to help in this.. thanks
Use:
#/([^/]*)$#
preg_match('#/([^/]*)$#', $str, $matches);
We first check for a slash. Then the capturing group is zero or more non-slash characters. Then, the end of the string. $matches[1] holds the result.
Why not simply match \d+$ - this will match any trailing number.
if (preg_match('/\d+$/', $subject, $match)) {
$result = $match[0];
} else {
$result = "";
}
If you want to match anything (even it it's not a number) after the last slash, just use [^/]+$ instead:
preg_match('#[^/]+$#', $subject, $match)

Categories