Extracting Strings on Multiline
Please share your ideas on this concern.
how do i extract the string Domain/username field on this multi threaded line.
Name Username
COMP01-100 Domain\user.one
Name Username
COMP02-100 Domain\test.user
Name Username
COMP10-100 Domain\sample.user
--
Note:
I should have searched for COMP02-100 and extract Domain\test.user as result.
I think I should have used the php preg_match_all function but i just can't find the right syntax on this.
Any ideas?
If I understand you correctly, you could get the desired result with these lines:
$search_string= 'COMP02-100';
preg_match("/$search_string (\w+\\\\\w+.\w+)/", $multiline, $matches);
preg_match is sufficient if you just want the first match
Related
I have the following HTML and what I want is to extract the string between two tags and replace another string with the one that was extracted.
Here is an example:
<p>|something=to remember:yes: no</p>
<p><A:keyword term="to remember, yes no" /></p>
I have built something like this but I do not know whether regular expressions really help me here?
<[p]\>(.*)<\/p>
Thanks for your help.
I solved my problem by using
something==([A-Za-z]*)\s([A-Za-z]*),\s([A-Za-z]*)\s([A-Za-z]*)
as a static input and then everything after it was treated with regex and grouping.
With that solution I did solve my problem.
I'm trying to scrap some information just for learning PHP and regex and I would like to extract it from an html.
The html text is an entire webpage but it has some patterns like somehtmltext_andtags_andeverything /ajax/hovercard/user.php?id=THE_ID_I_WANT andmore_text_and_tags.
I can isolate the pattern with TextEdit in Mac, but I want separate it!
how could I make it in PHP?
Thank you in advance!
Rafael.
Sorry, I was very unclear.
I want to separate only de ID, so if you see the image, the only text you would get is 100009799451329 . If the final result is the whole sentence (ajax/hovercard/user.php?id=100009799451329) it doesn't matter, goes fine for me!
try this
$matchArr = NULL;
preg_match_all("/\/ajax\/hovercard\/user\.php\?id=(.*?)\&/", $yourStr, $matchArr);
print_r($matchArr);
You can use the following pattern to find the id:
\/ajax\/hovercard\/user.php\?id=(\d+)
See a demo.
Explanation:
\/ajax\/hovercard\/user.php\?id= will match /ajax/hovercard/user.php?id=
(\d+) captures a sequence of digits, in this case the user id.
I'm making a function call to a library that is returning a malformed json array. I can work around this if I can get a preg written to extract the part that I want.
The array is a jumbled mess, but buried deep inside it is a string that looks like this:
token=??????,
I need to write a preg to grab the characters represented by the question marks. I wrote this, but it's not getting the part of the text that I want:
$token = preg_match('#^(?:token=)?([^,]+)#i', $badJson, $matches);
Can anyone help me? Thanks.
You can try:
/token=([^,]+)/i
and the use the first sub-match to extract the token. Being more specific is usually a good idea with regex (eg. does the token have a set length? does it only contain hex characters? etc.)
Site note: https://leaverou.github.io/regexplained/ is a great site for testing regular expressions.
I need some help creating regular expressions to pick information out of a file. I am using php preg_match to do it and am trying to get information that looks like the following:
ex.
19-Aug-2013 //The date will always be in the format.
and a number like this
303.00
The file I am trying to get this information from is the body of a mime type email.
I only need these two types of specific information.
Try this as a whole:
preg_match_all('/(\d{2}-\w{3}-\d{4}|\d+\.\d{1,2})/', $file, $matches);
\d{2}-\w{3}-\d{4}: Get the date
\d+\.\d{1,2}: Get the float
By using preg_match_all you return all found matches in all the haystack. preg_match only matches the first occurance.
For the date you can use:
[0-9]{2}-[a-z]{3}-[0-9]{4}
For the number:
[0-9]+\.[0-9]{2}
With no specific input is hard to come up with a better regex at the moment...
This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Identifying if a URL is present in a string
Php parse links/emails
I'm working on some PHP code which takes input from various sources and needs to find the URLs and save them somewhere. The kind of input that needs to be handled is as follows:
http://www.youtube.com/watch?v=IY2j_GPIqRA
Try google: http://google.com! (note exclamation mark is not part of the URL)
Is http://somesite.com/ down for anyone else?
Output:
http://www.youtube.com/watch?v=IY2j_GPIqRA
http://google.com
http://somesite.com/
I've already borrowed one regular expression from the internet which works, but unfortunately wipes the query string out - not good!
Any help putting together a regular expression, or perhaps another solution to this problem, would be appreciated.
Jan Goyvaerts, Regex Guru, has addressed this issue in his blog. There are quite a few caveats, for example extracting URLs inside parentheses correctly. What you need exactly depends on the "quality" of your input data.
For the examples you provided, \b(?:(?:https?|ftp|file)://|www\.|ftp\.)[-A-Z0-9+&##/%=~_|$?!:,.]*[A-Z0-9+&##/%=~_|$] works when used in case-insensitive mode.
So to find all matches in a multiline string, use
preg_match_all('/\b(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)[-A-Z0-9+&##\/%=~_|$?!:,.]*[A-Z0-9+&##\/%=~_|$]/i', $subject, $result, PREG_PATTERN_ORDER);
$result = $result[0];
Why not try this one. It is the first result of Googling "URL regular expression".
((https?|ftp|gopher|telnet|file|notes|ms-help):((\/\/)|(\\\\))+[\w\d:##%\/;$()~_?\+-=\\\.&]*)
Not PHP, but it should work, I just slightly modified it by escaping forward slashes.
source