Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
How could I get the specific substring between 'Y___(string i want to get)___N'?
For example:
"Y___INT_GET_ERROR_CONFIGS___N"
"INT_GET_ERROR_CONFIGS"
You can get everything inside by Y___(.*?)___N, you can use matchAll to get all instance that matches this case, and you can loop through and get the group value.
const str = `Y___INT_GET_ERROR_CONFIGS___N
INT_GET_ERROR_CONFIGS Y___INT_GET_ERROR_SOMETHING___N`
const result = str.matchAll(/Y___(.*?)___N/g);
for (match of result) {
console.log(match[1])
}
If it's just the first occurrence you wish you match, then:
'Y___(string you want to get)___N'.match(/(?<=Y___).+(?=___N)/)
Result:
"(string you want to get)"
If you want all such occurrences to be returned, then use the g flag:
`Y___(string you want to get)___N
.
.
.
Y___(second string you want)___N`.match(/(?<=Y___).+(?=___N)/g)
Result:
["(string you want to get)", "(second string you want)"]
Explanation:
(?<=Y___): A positive lookbehind stipulates that matches will be preceded by the contents of the lookbehind, namely "Y___". The contents of the lookbehind does not form part of the match result, and also does not consume characters during matching.
.+: Matches at least one instance of any character, but will match as many as possible.
(?=___N): A positive lookahead stipulates that matches will be proceeded by the contents of the lookahead, namely "___N". The contents of the lookahead does not form part of the match result, nor does it consume characters during matching.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I need validate a input using regex (i think it's better) but in this input can be one or more "sentences" and be [A-Z] size 1. How can i do that?
E.g.:
A,B,D,G,J,X no repeat letters but this validate i do in code. I think regex is better 'cause validate a entire sentence instead letter by letter using a loop and split. My english is rusty, appreciate some help to improve =)
Note, Assumption is you want a single letter
If you just want to validate:
if (/([A-Z]*)?,([A-Z]*),?/.test(subject)) {
// Successful match
} else {
// Match attempt failed
}
If you are using to get extract values:
result = subject.match(/([A-Z]*)?,([A-Z]*),?/g);
Maybe this can help you ([A-Z],)+[A-Z] it will match a serie of uppercase letter followed by comma, and end with uppercase letter :
regex demo
A,B,D,G,J,X -> matches
A,B,DE,G,J,X -> not matches
A,B,D,G,J,XY -> not matches
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have textbox and I want get string value. But I want users to not be able to enter the string that has number on first letter. As matter of fact I want to replace number with '' null.
for example
1test =====convert=======> test
you can simply use ^[a-zA-Z]
^ starts with a-z or A-Z
or if you want special character too then use ^\D
^\D : Matches anything other than a decimal digit
Regex Demo
you can use $text.replace(/^[^0-9]+/, '')
/^ beginning of the line
[^0-9]+ match anything other than digits at-least once
thanks # Wiktor and Tushar
here is the solution: You can check on this live regex.
https://regex101.com/r/OJfyv4/1
$re = '/\b[a-z][a-z0-9]*/';
$str = '1test';
preg_match_all($re, $str, $matches);
// Print the entire match result
print_r($matches);
This works your case:
^\d+
https://regex101.com/r/daezA9/1
^ asserts position at start of the string
\d matches a digit (equal to [0-9])
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How in javascript to make regex to recognize and extract integer numbers for coordinates which have format like
( number1, number2 )
between ( and number1 and , and number2 and ) can be arbitrary number of whitespaces (user are going to enter coordinates so I don't want to force strict format without whitespaces)
(\d+,\d+)
what to add to this so it works ?
There are a few choices, pending your actual input.
To match all "whitespace characters" (space, tab, carriage return, newline and form feed), you can use the \s shorthand approach. If you want a number, in this case \d+, to be surrounded by "0 or more" of these, you would use:
\s*\d+\s*
In your full pattern:
\( # opening parentheses
\s*\d+\s*, # first number followed by a comma
\s*\d+\s* # second number
\) # closing parentheses
Note: The parentheses are escaped here as they're special characters in a regular expression pattern.
Now, if you don't want to match "all whitespace" and were only interested in plain spaces, for example, you could use a matching character set of [ ] (i.e. a space between two brackets). In the pattern from above:
\(
[ ]*\d+[ ]*,
[ ]*\d+[ ]*
\)
Not really sure how you want to use the matches, I'm assuming you want the numbers returned individually so in that case, you can use the following:
var str = '(1, 2)';
var matches = str.match(/\(\s*(\d+)\s*,\s*(\d+)\s*\)/);
if (matches) {
var firstNumber = matches[1];
var secondNumber = matches[2];
// do stuffs
}
Note: In the pattern I used here, I've wrapped the \d+s in parentheses; this will "capture" those values in to groups which are then accessible by their "group index". So, the first (\d+) will be available in matches[1] and the second will be available in matches[2].
Try this regex: \(\s*\d+\s*,\s*\d+\s*\).
Fiddle
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to test whether a string contains only punctuation symbols except one character symbol say hypen (-)
'??...' -> true
'.!sf' -> false
'..-??' -> false
I use library XRegEx library that allows to match for example punctuation with p{P} but I need to exclude some characters from this match.
I use following pattern:
new XRegExp("^\\p{P}+$")
How can I except hypen symbol from "-" this match?
N.B. Original question was about "leters":
I need to test whether a string contains letters except one character say letter "m"
/^[a-ln-z]+$/.test('abcfx')
// true
/^[a-ln-z]+$/.test('abcfx12.!')
// false
/^[a-ln-z]+$/.test('abcmfx')
// false
Using positive lookahead:
/^(?=[^m]+$)[a-z]+$/.test('abcfx')
// true
/^(?=[^m]+$)[a-z]+$/.test('abcfx12.!')
// false
/^(?=[^m]+$)[a-z]+$/.test('abcmfx')
// false
What you are looking for is probably this regex, which accepts ranges from a to l and from n to z
^[a-ln-z]+$
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to test if a string can match a pattern in javascript
problem is there is a random string in the middle
pattern: "account/os/some_random_string/call_back"
so a string look like below will match
var mystring = "account/os/1234567/call_back"
thanks
You want a regex for starts with account/os/ and ends with /call_back, here's one:
/^account\/os\/.*\/call_back$/
.* will match any random string (including the empty string.). If you want a minimum length on the random string you change the *:
.* : 0 or more characters
.+ : 1 or more characters
.{n,} : n or more characters (replace n with an actual number)
Well, it depends. If you want every single character between account/os/ and /call_back, use this:
var randomStr = mystring.match(/account\/os\/(.*)\/call_back/)[1];
The match will return an array with 2 elements, the first one with the entire match, the 2nd one with the group (.*). If you are completely sure that you have at least one character there, replace * with +.
If you know something more specific about the text you have to collect, here are some replacements for the .(dot is matching almost everything):
[A-z] for any of A, B, .. , Y, Z, a, b, .. , y, z
[0-9] for any digit
You can mix then and go fancy, like this:
[A-Ea-e0-36-8]
So, your pattern may look like this one:
/account\/os\/([A-Ea-e0-36-8]*)\/call_back/
Your example have a number there, so you are probably looking for:
/account\/os\/([0-9]*)\/call_back/
or
/account\/os\/(\d*)\/call_back/
.. it's the same thing.
Hope that helps.
Edit: What JS answer doesn't have a jsfiddle? http://jsfiddle.net/U2Jhw/