regular expression match numeric value - javascript

I'm trying to match a pattern:
show_clipping.php?CLIP_id=*
from:
a href="javascript:void(0);" onclick="MM_openBrWindow('show_clipping.php?CLIP_id=575','news','scrollbars=yes,resizable=yes,width=500,height=400,left=100,top=60')">some text</a>
where
*
can be only numeric values(eg: 0, 1 , 1234)
the result has to return the whole thing(show_clipping.php?CLIP_id=575)
what I've tried:
show_clipping.php\?CLIP_id=([1-9]|[1-9][0-9]|[1-9][0-9][0-9])
but my attempt would truncate the rest of the digits from 575, leaving the results like:
show_clipping.php?CLIP_id=5
How do I match numeric part properly?
Another issue is that the value 575 can contain any numeric value, my regex will not work after 3 digits, how do i make it work with infinit amount of digits

You didn't specify what language your are using so here is just the regex:
'([^']+)'
Explanation
' # Match a single quote
([^`])+ # Capture anything not a single quote
' # Match the closing single quote
So basically it capture everything in single quotes, show_clipping.php?CLIP_id=5 is in the first capture group.
See it action here.
To only capture show_clipping.php?CLIP_id=5 I would do '(.*CLIP_id=[0-9]+)'
' # Match a single quote
(.* # Start capture group, match anyting
CLIP_id= # Match the literal string
[0-9]+) # Match one of more digit and close capture group
' # Match the closing single quote

Answer:
^(0|[1-9][0-9]*)$
answered before:
Regex pattern for numeric values
(answer number 6)

What about this?
onclick.match(/show_clipping\.php\?CLIP_id=\d+/)
["show_clipping.php?CLIP_id=575"]
(From the tags of your question I assume you're using JavaScript)

show_clipping.php\?CLIP_id=(\d+)
\d matches a digit, and + means 1 or more of them.

How about:
/(show_clipping.php\?CLIP_id=[1-9]\d*)/

Related

REgex for non repeating alphabets comma seperated

I have a requirement where I need a regex which
should not repeat alphabet
should only contain alphabet and comma
should not start or end with comma
can contain more than 2 alphabets
example :-
A,B --- correct
A,B,C,D,E,F --- correct
D,D,A --- wrong
,B,C --- wrong
B,C, --- wrong
A,,B,C --- wrong
Can anyone help ?
Another idea with capturing and checking by use of a lookahead:
^(?:([A-Z])(?!.*?\1),?\b)+$
You can test here at regex101 if it meets your requirements.
If you don't want to match single characters, e.g. A, change the + quantifier to {2,}.
The statement of the question is incomplete in several respects. I have made the following assumptions:
Considering that D,D,A is incorrect I assume that a letter cannot be followed by a comma followed by the same letter.
The string may contain the same letter more than once as long as #1 is satisfied.
Considering that A,,B,C is incorrect I assume a comma cannot follow a comma.
Since the examples contain only capital letters I will assume that lower-case letters are not permitted (though one need only set the case-indifferent flag (i) to permit either case).
We observe that the requirements are satisfied if and only if the string begins with a capital letter and is followed by a sequence of comma-capital letter pairs, provided that no capital letter is followed by a comma followed by the same letter. We therefore can attempt to match the following regular expression.
^(?:([A-Z]),(?!\1))*[A-Z]$
Demo
The elements of the expression are as follows.
^ # match beginning of string
(?: # begin a non-capture group
([A-Z]) # match a capital letter and save to capture group 1
, # match a comma
(?!\1) # use negative lookahead to assert next character is not equal
# to the content of capture group 1
)* # end non-capture group and execute it zero or more times
[A-Z] # match a capital letter
$ # match end of string
Here is a big ugly regex solution:
var inputs = ['A,B', 'D,D,D', ',B,C', 'B,C,', 'A,,B'];
for (var i=0; i < inputs.length; ++i) {
if (/^(?!.*?([^,]+).*,\1(?:,|$))[^,]+(?:,[^,]+)*$/.test(inputs[i])) {
console.log(inputs[i] + " => VALID");
}
else {
console.log(inputs[i] + " => INVALID");
}
}
The regex has two parts to it. It uses a negative lookahead to assert that no two CSV entries ever repeat in the input. Then, it uses a straightforward pattern to match any proper CSV delimited input. Here is an explanation:
^ from the start of the input
(?!.*?([^,]+).*,\1(?:,|$)) assert that no CSV element ever repeats
[^,]+ then match a CSV element
(?:,[^,]+)* followed by comma and another element, 0 or more times
$ end of the input
This one could suit your needs:
^(?!,)(?!.*,,)(?!.*(\b[A-Z]+\b).*\1)[A-Z,]+(?<!,)$
^: the start of the string
(?!,): should not be directly followed by a comma
(?!.*,,): should not be followed by two commas
(?!.*(\b[A-Z]+\b).*\1): should not be followed by a value found twice
[A-Z,]+: should contain letters and commas only
$: the end of the string
(?<!,): should not be directly preceded by a comma
See https://regex101.com/r/1kGVSB/1

Regular Expression to match text between # and only if # is not preceded by '

Hello I'm trying to find a regular expression that can help me find all matches inside a string when they're inside # and only if # are not preceded by an apostrophe "'".
Basically I need to bold the text just as here when we use double * to bold text like this, but the apostrophe should work as an escape character.
For example
#Hello my name is Noé# should look like Hello my name is Noé
#Hello this has an escape apostrophe '# so I'll match until here# should look like Hello this has an escape apostrophe '# so I'll match until here
Inside a long text there might or might not be several matches:
"Hello I'm a text #I'm bold#, and I need to know how to match my text that's inside two '#, and #I will not match either 'cause I got no end"
So i can print it like
"Hello I'm a text I'm bold, and I need to know how to match my text that's inside two '#, and #I will not match either 'cause I got no end"
If thats not possible with a RegExp I could program a finite state machine, but I was hoping I was possible, thank you in advance God bless you!
Note: I will handle the escape characters later by now I just need to know how to mach this
/(?<!')#.*(?<!')#/gim
This was the only thing I could come up with, but honestly, I have no idea how negative look behind works :(, with this regexp it would match wrong. For example, if I type:
"I'm a text #and I should be a match# and this should not #But this should as well# and I'm just some random extra text"
matches from the first # occurrence until the last one, like so:
"I'm a text #and I should be a match# and this should not #But this should as well# and I'm just some random extra text"
I think this should work:
(?<!')#(.*?)(?<!')#
Here you can see the regexp working with your examples: https://regex101.com/r/wnguiA/1
(?<!') is Negative Lookbehind, it tells the regex engine to temporarily step backwards in the string, to check if the text inside the lookbehind can be matched there. (?<!a)b matches a b that is not preceded by an a.
More easy is the (.*?) that matches any character (except for line terminators); adding ? tells the capturing group to be not-greedy and stop at the first occourence of the succesive token.
To prevent triggering the negatilve lookbehind at all the positions not asserting a ' to the left, you can also first match # and do the assertion after it.
#(?<!'#)(.*?)#(?<!'#)
Regex demo
Another option instead of using the non greedy .*? is to use a negated character class matching any char except #
Then when you encounter # only match it if there is ' before it using a positive lookbehind.
#(?<!'#)([^#\n]*(?:#(?<='#)[^#\n]*)*)#(?<!'#)
#(?<!'#) Match # not directly preceded by '
( Capture group 1
[^#\n]* Optionally match any char except # or a newline
(?: Non capture group
#(?<='#) Match # not directly preceded by '
[^#\n]* Match optional repetitions of any char except # or a newline
)* Close non capture group and optionally repeat it to match all occurrences
) Close group 1
#(?<!'#) Match # not directly preceded by '
Regex demo

Regex matching a string pattern and number ( url format )

I have a string that follows this url pattern as
https://www.examples.org/activity-group/8712/activity/202803
// note : the end ending of the url can be different
https://www.examples.org/activity-group/8712/activity/202803‌​?ref=bla
https://www.examples.org/activity-group/8712/activity/202803‌​/something
I'm trying to write a regex that matches
https://www.examples.org/activity-group/{number}/activity/{number}*
Where {number} is an integer of length 1 to 10.
How to define a regex that checks the string pattern and checks if the number is at the right position in the string ?
Background: in Google form, in order validate an answer , I want to enforce people to enter an url in this format. Hence the use of this regular expression.
For Urls not matching that format, the regex should return false. For example : https://www.notthesite.org/group/8712/activity/astring
I went through several examples, but they match only if the number is present in the string.
Examples sources :
How to find a number in a string using JavaScript?
Get the first Int(s) in a string with javascript
^https:\/\/www\.examples\.org\/activity-group\/[0-9]{1,10}\/activity\/[0-9]{1,10}(\/[a-z]+)*((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)*$
^ - start of string
\ - escape character
[0-9] - a digit
{1,10} - between one and ten of the previous items
(\/[a-z]+)* - Allow additional URL segments
((\?[a-z]+=[a-zA-Z0-9]+)(\&[a-z]+=[a-zA-Z0-9]+)*)* - Allow query parameters with first parameter using a ? and all others using &
$ - end of string
This is assuming the URL segment and query parameter keys are lowercase letters only. The query parameter values can be lowercase letters, uppercase letters, or digits.
You could use
https?:\/\/(?:[^/]+\/){2}(\d+)\/[^/]+\/(\d+)
See a demo on regex101.com.
Broken down, this says:
https?:\/\/ # http:// or https://
(?:[^/]+\/){2} # not "/", followed by "/", twice
(\d+) # 1+ digits
\/[^/]+\/ # same pattern as above
(\d+) # the other number
You'll need to use group 1 and 2, respectively.
If this is too permissive, use
https:\/\/[^/]+\/activity-group\/(\d+)\/activity\/(\d+)
Which reads
https:\/\/[^/]+ # https:// + some domain name
\/activity-group\/ # /activity-group/
(\d+) # first number
\/activity\/ # /activity/
(\d+) # second number
See another demo on regex101.com.
Probably you need something like:
(http[s]?:\/\/)?www.examples.org\/activity-group\/(\d{1,10})\/activity\/(\d{1,10})([\S]+?)$
Where:
(http[s]?:\/\/)? matches any http:// or https:// part.
www.examples.org is your domain name.
(\d{1,10}) will match the first integer with max len of 10(after activity-group).
Second (\d{1,10}) will match the second integer after activity.
And finally ([\S]+?)$ will match any optional data after the second number until a new line is found, assuming that you use multiline flag with \m.
Check it at http://regexr.com/3h448
Hope it helps!

regular expression to replace with ','

I have one RegExp, could anyone explain exactly what it does?
Regexp
b=b.replace(/(\d{1,3}(?=(?:\d\d\d)+(?!\d)))/g,"$1 ")
I think it is replacing with space(' ')
if i'm right, i want to replace it with comma(,) instead of space(' ').
To explain the regex, let's break it down:
( # Match and capture in group number 1:
\d{1,3} # one to three digits (as many as possible),
(?= # but only if it's possible to match the following afterwards:
(?: # A (non-capturing) group containing
\d\d\d # exactly three digits
)+ # once or more (so, three/six/nine/twelve/... digits)
(?!\d) # but only if there are no further digits ahead.
) # End of (?=...) lookahead assertion
) # End of capturing group
Actually, the outer parentheses are unnecessary if you use $& instead of $1 for the replacement string ($& contains the entire match).
The regex (\d{1,3}(?=(?:\d\d\d)+(?!\d))) matches any 1-3 digits ((\d{1,3}) that is followed by a multiple of 3 digits ((?:\d\d\d)+), that isn't followed by another digit ((?!\d)). It replaces it with "$1 ". $1 is replaced by the first capture group. The space behind it is... a space.
See regexpressions on mdn for more information about the different syntaxes.
If you want to seperate the numbers with a comma, instead of a space, you'll need to replace it with "$1," instead.
Don't try to solve everything by using regular expressions.
Regular expressions are meant for matching, not to fix non-text-encoded-as-text formatting.
If you want to format numbers differently, extract them and use format strings to reformat them on a character processing level. That is just an ugly hack.
It is okay to use regular expressions to find the numbers in the text, e.g. \d{4,} but trying to do the actual formatting with regexp is a crazy abuse.

Regex get all text from # to quotation

Okay so I currently have:
/(#([\"]))/g;
I want to be able to check for a string like:
#23ad23"
Whats wrong with my regex?
Your regex (/(#([\"]))/g) breaks down like this:
without start/end delimiters/flags and capturing braces..
#[\"]
which just means #, followed by ", but the square brackets for the class are unnecessary, as there is only one item, so equivalent to...
#"
I think you want to match all characters between # and " inclusive (and captured exclusively).
Start with regex like this:
#.+?"
Which means # followed by anything (.) one or more times (+) un-greedily (?) followed by "
so with the capturing brackets, and delimeters...
/(#(.+?)")/g
Is this how you mean?
/(#([^\"]+))/g;
This will include everything until it reaches the " char.
For minimum match count (bigger-length matches): #(.+)\"
For maximum match count (smaller-length matches): #(.+?)\"

Categories

Resources