RegEx to find a pattern [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Looking for regex to do the following in php:
Identify if the given string is in the pattern string1_string2_number e.g ph_val_34563, ph_val_296987 etc.
When true, extract the number part.

regex = "^[A-Za-z]+_[A-Za-z]+_(\d+)$"
Assuming that the valid characters for your strings are letters, [A-Za-z]+ says to expect a group of one or more letters. The _ following these character classes says that an _ must follow.
(\d+) says to group, and capture, a set of one or more numbers following the previous expression.
^ says: "begins with"
$ says: "ends with"
You should take a look at a tutorial on regular expressions.

Related

Complex Regex Pattern Combining Numbers with Letters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
To search for a specific string, which is like UE-42F5070, in a text with preg_match function I am trying to write a regular expression. I am not able to write the regular expression pattern for "UE-##F####", where #'s are numbers, to use with preg_match to find them in the text.
Example of the string;
UE-32F5070
UE-50F8000
You can use this regex:
'/^UE-\d{2}F\d{4}$/'
Recommended Reading on Regex
Details:
^ - Start input
UE - Match literal "UE"
\d{2} - Match 2 digits
F - Match literal "F"
\d{4} - Match 4 digits
$ - End input
A number in regex is [0-9] or \d. Repetitions are {n}. So
UE-[0-9]{2}F[0-9]{4}
Or
UE-\d\dF\d\d\d\d
It looks like UE is a constant, so you can start your regex with that as a literal, the rest should just be:
preg_match('/UE-([0-9]{2}F[0-9]{4})/', $string, $matches);

php Regular Expression match custom [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm not very good at regular expressions. Please help make the expression.
$subject = "action[attribute1=value1,attribute2=values,...]";
// format is ^word[str=str,...]$
I need to match "action", "attributes" and "values". thanks.
First find a match with regex pattern \b(\w+)\[((?:(?<=[,\[])(?:\w+)=(?:[^,\]]+)[,\]]?)+)]
to get action name as Group 1 and parameter list as Group 2.
In next step apply regex pattern (?:^|(?<=,))(\w+)=([^,\]]+)(?=,|$) to Group 2 from above regex
to get a list of attributes and associated values...

Regular expression needed for "<!-- A-Za-z0-9 -->" [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Need help with regular expression for <!-- A-Za-z0-9 --> where all the uppercase, smallcase and digits are allowed. Need a regular expression to find this chunk including the comment tags (<!-- -->).
[<!\-\-A-Za-z0-9\-\->]
This is a character class and the order doesn't matter in a character class. What will work however:
<!--\s*[A-Za-z0-9]+\s*-->
If you now want to include spaces inside the comment, you can use:
<!--\s*[A-Za-z0-9 ]+\s*-->
I used the \s* in case there are several white spaces between the <!-- and --> and the part to capture.
Use preg_match_all and if you want to make the regex shorter, you can use the i flag. something like this:
preg_match_all('~<!--\s*[a-z0-9]+\s*-->~i', $page, $matches);
This is all I can think of according to your question
#<!--([A-Za-z0-9]*)-->#

Build my specific REgular Expression [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to build a regular expression for a specific problem but I cannot achieve to do it. I have a structure of a string like this 01422756860-8.
As you can see there is a string of length 13. The first 11 characters should be numbers in the interval of [0-9], the next character is a line(minus) and the last character is again a number. There shouldn be any space in between these characters.
Could anyone please help me to do this? I am going to use this regex in php.
I look forward on your answer.
Thank You.
What's the problem?
/^[0-9]{11}-[0-9]$/
Demo
Autopsy:
^ The string must start here
[0-9]{11} Any digit from 0 to 9 repeated exactly 11 times
- A literal dash
[0-9] Any single digit from 0 to 9
$ The string must end here
This regular expression should do it
\d{11}-\d

Please decode this regular expression for me [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I inherited some php code for a webgame that I am trying to modify. I came across this line, and I can't figure out what it is supposed to be doing. Could someone help me out? $notice is just a regular string.
$notice = preg_replace("#([\S]{60})#i", "\\1 ", $notice);
It will find any continuous sequence of 60 non-whitespace characters in $notice, and insert a space after it:
(..) creates a capture group. Because it's the first group it's referred to as \1 in the replacement string. Because the whole pattern is in the group it's not really needed here.
[..] create a character class, but because it contains only one meta-character, it's not really needed here, either.
\S matches any non-whitespace character
{60} is a quantifier; it means 'repeated 60 times'.
This code is equivalent to:
$notice = preg_replace("#\S{60}#i", "\\0 ", $notice);

Categories