preg-match

Adding dollar sign before each string in array?

大兔子大兔子 提交于 2019-12-02 16:35:18
问题 I have this string: $str = "(he+is+genius*2)/clever"; which looks like this in array; Array ( [0] => ( [1] => he [2] => + [3] => is [4] => + [5] => genius [6] => ) [7] => * [8] => and [9] => / [10] => clever ) What I want to do is placing dollar sign $ before each string present in $str but ignoring non-alphanumeric and numbers. At the end i want to have something which looks like this; $newstr = "($he+$is+$genius*2)/$clever"; 回答1: For each value, check if the first char (or the whole value)

Preg_match: get last two domain segments from url in one expression

六月ゝ 毕业季﹏ 提交于 2019-12-02 15:09:41
问题 There is an example on php.net how to get last two domain segments in two steps: <?php //get host name from URL preg_match("/^(http:\/\/)?([^\/]+)/i", "http://www.php.net/index.html", $matches); $host = $matches[2]; // get last two segments of host name preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches); echo "domain name is: {$matches[0]}\n"; /* Output is php.net */ ?> But how can I do it in one step, using only one preg_match expression? 回答1: This piece of code: $domain = 'http://www.php

Regular Expression to Split String based on space and matching quotes in java

别来无恙 提交于 2019-12-02 14:33:16
问题 I have a String which i need to split based on the space and the exact matching quotes. If the string = "It is fun \"to write\" regular\"expression" After the Split i want the result to be : It is fun "to write" regular "expression The regular expression from which i came to some thing close to do this was : STRING_SPLIT_REGEXP = "[^\\s\"']+|\"([^\"]*)\"|'([^']*)'" Thanks in advance for answers. 回答1: It seems that you just used regex from this answer, but as you could see it doesn't use split

Why downloading the youtube file id does not work?

ε祈祈猫儿з 提交于 2019-12-02 13:33:53
I found the code that gets the movie id from the youtube site, the script works well natomast if the ID has a hyphen "-" the script does not get the ID from the url. I'm weak in regular expressions but still tried to convert the expression, but I can not deal with it. Can you direct me or show me what error I'm doing? Thanks My code: $links = array( 'https://www.youtube.com/watch?v=-SXKV0jDxuA', 'https://www.youtube.com/watch?v=ylfhCpi9AEU' ); foreach ($links as $link){ preg_match("#([\/|\?|&]vi?[\/|=]|youtu\.be\/|embed\/)(\w+)#", $link, $matches); var_dump(end($matches)); } //result =>

Regex to match 2 or more words

冷暖自知 提交于 2019-12-02 13:14:26
问题 I have a regex that tries to match for 2 or more words, but it isn't working as it's suppose to. What am I doing wrong? $string = "i dont know , do you know?"; preg_match("~([a-z']+\b){2,}~", $string, $match); echo "<pre>"; print_r($match); echo "</pre>"; Expected Result: Array ( i dont know ) Actual Result: Array ( ) 回答1: This will match for string that contains 2 words or more exactly: /([a-zA-Z]+\s?\b){2,}/g you can go http://www.regexr.com/ and test it PHP: $string = "i dont know , do you

Regex to match a word, even is there are spaces between letters

痞子三分冷 提交于 2019-12-02 13:09:31
I'd like to have a regex to match a word, even if there are spaces between the characters. When I want to match the word test , it should match the following: test t est t e s t And so on, but it should not match things like this: tste te ts s tet I have this regex: (t[\s]*e[\s]*s[\s]*t[\s]*) But I don't believe that this one is very efficient. Actually, it is the same as t\s*e\s*s\s*t (if the word appears inside a larger string, \bt\s*e\s*s\s*t\b is preferable). This is the only way to match such words. You have to consume these spaces, otherwise you won't have a match. Why not remove all

How to get span tag content using preg_match function? [closed]

怎甘沉沦 提交于 2019-12-02 12:57:57
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I have the following HTML code: <span role="button" class="a-n S1xjN" tabindex="0">414,817 people</span> How to use preg_match function to get the 414817 number? 回答1: The Better Solution A regex is the wrong tool here. HTML is not a regular language, and cannot be accurately parsed using regular expressions. Use

preg_split() String into Text and Numbers [duplicate]

假如想象 提交于 2019-12-02 11:10:52
问题 This question already has answers here : Split String into Text and Number (2 answers) Closed 5 years ago . i want to split the string into numbers and text as separate. print_r(preg_split("/[^0-9]+/", "12345hello")); output: Array ( [0] => 12345 [1] => ) 回答1: By using [^0-9]+ you are actually matching the numbers and splitting on them, which leaves you with an empty array element instead of the expected result. You can use a workaround to do this. print_r(preg_split('/\d+\K/', '12345hello'))

Get all video qualities from a block of text using a regex

一世执手 提交于 2019-12-02 10:39:51
I want to get all video qualitys from string. My string: #EXTM3U #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=350000 128/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=750000 500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1000000 750/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1250000 1000/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94 #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1750000 1500/prog_index.m3u8?key=49bfee85b05d117a2906368428094e94 #EXT

Capture groups match with quantifier Regexp

假如想象 提交于 2019-12-02 09:30:23
I am newbie in regex world, I need to capture some different types of strings. By the way please suggest more elagant way to capture such strings. n = any positive number(not the same) |n||0||0||0||0| |n||n||0||0||0| |n||n||n||0||0| |n||n||n||n||0| |n||n||n||n||n| I have tried to use such regular expression for capturing first and secodn types of strings ^\|([1-9]+)\|(?:([1-9]+)\|){4}|(?:(0)\|){4}$ Zero should be treated as separate char, I need to capture each number or zero The problem now that it only captures first matched character and last one But doesn't capture other digits Please help