Multiple max lengths in a regular expression - regex

I have the following regular expression:
[0-9]{7}-[0-9]{1}$
I should be able to match the following patterns:
1234567-8
3142539-1
But not the following:
12345678-1
1234567-12
Currently my regex matches 12345678-1 but not 1234567-12 (in JavaScript). Both should fail. What am I doing wrong?

Your pattern would match any string that ends($) with [0-9]{7}-[0-9]{1} and so it would match those inputs..
Use ^(start of the string) to specify that you want to match exactly..
^[0-9]{7}-[0-9]{1}$

Related

regex parse multiple expressions

I have a requirement to parse part of url with multiple expressions using regex, expressions are like /abc/def (or) /z/a (or) /v/g. I have a regex that satisfies single expression, but not sure how to do for multiple expression.
(?<volga>.+?\/abc\/def.+?)
volga is a named capturing group.
Above regular expression satisfies anything before /abc and anything after /def, the same way url also has /z/a and /v/g. This is kind of either /abc/def/ or /z/a or /v/g can exist in url. How to write a regular expression that checks for all of above expressions.
Examples of url:
/api/com/abc/def/710660716/847170/
/api/com/z/a/
Maybe this:
(?<volga>.+?\/((abc\/def)|(z\/a)|(v\/g)).*)
Matches any string containing
abc/def
or z/a
or v/g
Regex101

Regular Expressions (pcre) for shortcode/bbcode

I have a regex (see on https://regex101.com/r/mB7vQ8/2):
/\[content_box((.*?)!?\])(.*?)\[\/content_box\]/ig
for match all [content_box] (with or without tag parameters) in a text like:
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
My regex work, but if [content_box] is included in a [content_boxes] the rule fails the match (in strong):
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
the expected match is:
[content_boxes foo=bar][content_box baz=foo]text[/content_box][/content_boxes]
[content_box]text[/content_box]
[content_box foo=bar]text[/content_box]
see online https://regex101.com/r/mB7vQ8/2
How solve it?
You can use this regex with word boundaries:
~\[content_box\b\s*([^]]*)\](.*?)\[/content_box\]~
RegEx Demo
Here content_box\b will not match content_boxes and match will always be inner [content_box ..] tag.

Finding a pattern with optional end using regular expression

I am looking for one single regular expression to extract a block of text, which can be surrounded with an optional end. The challenge here is just to use a single regular expression.
The input is as follows:
Anchor: This is the text I want to extract A/C : 2015-5-20
Anchor: This is the text I want to extract
I am currently using the following regular expression
Anchor:(?<extact>.*)(A\/C)
The result looks as follows:
If I make the A/C block optional, Anchor:(?<extact>.*)(A\/C)? using a ? the matching gets to long:
It looks as follows:
Any ideas how to elegantly solve this with a single regex. An additional constraint is that I want to have a named block in the regex, (here extact)
You can find the sample code on regex101: https://regex101.com/r/wH5iQ4/1
Anchor:(?<extact>.*?)\s*(?=A\/C|$)
You can make use of lookahead here.See demo.
https://regex101.com/r/wH5iQ4/3

Regular Expression to unmatch a particular string

I am trying to use regular expression in Jmeter where in I need to unmatch a particular string. Here is my input test string : <activationCode>insvn</activationCode>
I need to extract the code insvn from it. I tried using the expression :
[^/<activationCode>]\w+, but does not yield the required code. I am a newbie to regular expression and i need help with this.
Can you use look-behind assertion in jmeter? If so, you can use thatr regex which will give you a word that follows <activationCode>
(?<=\<activationCode\>)\w+
If your input string is encoded (e.g for HTML), use:
(?<=\<activationCode\>)\w+
When designing a regular expression in any language for something like this you can match your input string as three groups: (the opening tag, the content, and the closing tag) then select the content from the second group.

php regex to match three words if not then two and then one

Q1: I'm writing a regex in php and not successful. I want to match the following:
so i would
if not then match:
so i
and then:
i would
and
so
i
would
Here is my code:
\b(so i|i would|so i would|(so|i|would))\b
Its only matching the: so, i, would, so i, i would .... but not matching the so i would?
Order your regex correctly.
\b(so i would|so i|i would|(so|i|would))\b
Put the longest string to match to the left.
The | is left-associative and hence, in your version Of the regex, is matching the shorter string.
Just put it at the beginning
\b(so i would|so i|i would|(so|i|would))\b
put longest pattern to left in the group: \b(long|...|short)\b
another solution: \b(so i would|i would|would|so i|so|i)\b
p.s. this is NFA regex engine feature, please refer to "Mastering Regular Expressions"