I have a Google form field, that contains 1 or more Id's
Patterns:
The IDs are always 6 numbers.
If only one ID is entered, a comma and a space is NOT required.
If more than one ID is entered, a comma and a space is required.
If more than one ID is entered, the last ID, should not have a comma or a space at the end.
Allowed Examples:
a single ID: 123456
multiple ID: 123456, 456789, 987654
Here is my current REGEX (does not work correctly)
[0-9]{6}[,\s]?([0-9]{6}[,\s])*[0-9]?
What am I doing wrong?
With your shown samples, could you please try following.
^((?:\d{6})(?:(?:,\s+\d{6}){1,})?)$
Online demo for above regex
Explanation: Adding detailed explanation of above regex.
^( ##Checking from starting of value, creating single capturing group.
(?:\d{6}) ##Checking if there are 6 digits in a non-capturing group here.
(?: ##Creating 1st non-capturing group here
(?:,\s+\d{6}) ##In a non-capturing group checking it has comma space(1 or more occurrences) followed by 6 digits here.
){1,})? ##Closing 1st non-capturing group here, it could have 1 or more occurrences of it.
)$ ##Closing 1st capturing group here with $ to make sure its end of value.
You can use
^\d{6}(?:,\s\d{6})*$
^ Start of string
\d{6} Match 6 digits
(?: Non capture group to repeat as a whole
,\s\d{6} Match a , a whitespace char and 6 digits
)* Close group and optionally repeat
$ End of string
Regex demo
Related
Is it possible to get the result just first two digits without % in the first group. Iam using Telegraf with Grafana.
Example:
5 Secs ( 22.3463%) 60 Secs ( 25.677%) 300 Secs ( 21.3522%)
Result:
22
I found out this regex in the similar topic, but it's return bad format for me :
^\s*\d+\s+Secs\s*\(\s*(\d+(?:\.\d+)?%)\)\s+\d+\s+Secs\s+\(\s+(\d+(?:\.\d+)?%)\)\s+\d+\s+Secs\s+\(\s+(\d+(?:\.\d+)?%)\)$
You can update your pattern to use a single capturing group by relocating the parenthesis around the digits only for the first occurrence.
You can omit the second and third capture groups as you don't need them.
^\s*\d+\s+Secs\s*\(\s*(\d+)(?:\.\d+)?%\)\s+\d+\s+Secs\s+\(\s+\d+(?:\.\d+)?%\)\s+\d+\s+Secs\s+\(\s+\d+(?:\.\d+)?%\)$
^ ^
Regex demo
Or you might use a named capture group, for example digits
^\s*\d+\s+Secs\s*\(\s*(?P<digits>\d+)(?:\.\d+)?%\)\s+\d+\s+Secs\s+\(\s+\d+(?:\.\d+)?%\)\s+\d+\s+Secs\s+\(\s+\d+(?:\.\d+)?%\)$
With your shown samples, please try following regex.
^\d+\s+Secs\s+\(\s+(\d+)(?:\.\d+%)?\)(?:\s+\d+\s+Secs\s+\(\s+\d+(?:\.\d+)?%\))*
Online demo for above regex
Explanation: Adding detailed explanation for above.
^\d+\s+Secs\s+\(\s+ ##From starting of value matching digits(1 or more occurrences) followed by space(s) Secs spaces ( spaces.
(\d+) ##Creating 1st and only capturing group where we have digits in it.
(?:\.\d+%)?\) ##In a non-capturing group matching dot digits % ) keeping it optional followed by )
(?: ##Creating a non-capturing group here.
\s+\d+\s+Secs\s+\(\s+\d+ ##matching spaces digits spaces Secs spaces ( spaces digits
(?:\.\d+)? ##In a non-capturing group matching dot digits keeping it optional.
%\) ##matching % followed by ) here.
)* ##Closing very first non-capturing group, and matching its 0 or more occurrences.
If it's just the 1st occurrence you're after, wouldn't the following work?
/secs\s*\(\s*(\d+)/i
I am performing a string search where I am looking for the following three strings:
XXX-99-X
XXX-99X
XXX99-X
So far I have:
([A-Z]{3}(-?)[0-9]{2}(-?)[A-Z]{1})
How do I enforce that - has to be present at least once in either of the two possible locations?
You might use an alternation, to match either a - and optional - at the left or - at the right part.
Note that you can omit {1} from the pattern.
^[A-Z]{3}(?:-[0-9]{2}-?|[0-9]{2}-)[A-Z]$
^[A-Z]{3}
(?: Non capture group
-[0-9]{2}-?|[0-9]{2}- Match either - 2 digits and optional - Or 2 digits and -
) Close non capture group
$ end of string
regex demo
Or use a positive lookahead to assert a - at the right
^(?=[^-\r\n]*-)[A-Z]{3}-?[0-9]{2}-?[A-Z]$
^ Start of string
(?=[^-\r\n]*-) Positive lookahead, assert a - at the right
[A-Z]{3}-? Match 3 chars A-Z and optional -
[0-9]{2}-? Match 2 digits and optional -
[A-Z] Match a single char A-Z
$ End of string
Regex demo
With your shown samples, please try following.
^[A-Z]{3}(?:-?\d{2}-|-\d{2})[A-Z]+$
online demo for above regex
Explanation: Adding detailed explanation for above.
^[A-Z]{3} ##Matching if value starts with 3 alphabets here.
(?: ##Starting a non capturing group here.
-?\d{2}- ##Matching -(optional) followed by 2 digits followed by -
|
-\d{2} ##Matching dash followed by 2 digits.
) ##Closing very first capturing group.
[A-Z]+$ ##Matching 1 or more occurrences of capital letters at the end of value.
Hello folks I have a line like that in my file.
> **Keywords** : test, test2, test3
And I need to select keyword by keyword and all array with regex.
NOTE: That test elements can be more than 3
Group 1 : test, test2, test3
Group 2 : test
Group 3 : test2
Group 4 : test3
I try to write that regex but it's not repeated for all commas :(
/^(> \*\*Keywords\*\* : ),?([\w]+)/gmi
This is the test env : https://regex101.com/r/UHLrX1/2
How can I handle that regex?
In Javascript, you may use this regex with a lookbehind assertion:
(?<=(^> \*\*Keywords\*\* : )(?:\w+, )*)(\w+)
RegEx Demo
RegEx Details:
(?<=: Start positive lookbehind
(^> \*\*Keywords\*\* : ): Match > \*\*Keywords\*\* : and capture it in group #1
(?:\w+, )*: Followed by 0 or more comma separated words
): End positive lookbehind
(\w+): Match 1+ character word in capture group #2
EDIT: In case you want to capture more than 3 elements as per shown samples then one could try following regex:
^\*\*Keywords\*\*.*?:\s+((?:(?:[^,]*),\s+){1,}(?:.*))$
Online demo for above regex
With your shown samples, please try following regex.
^\*\*Keywords\*\*.*?:\s+(([^,]*),\s+([^,]*),\s+(.*))$
Online demo for above regex
Explanation: Adding detailed explanation for above.
^^\*\*Keywords\*\*.*?:\s+ ##From starting of value matching till colon followed by spaces(1 or more occurrences)
( ##Starting 1st capturing group here.
([^,]*) ##In 2nd capturing group matching everything till comma comes.
,\s+ ##Matching comma followed by spaces 1 or more occurrences.
([^,]*) ##In 3rd capturing group matching everything till comma comes.
,\s+ ##Matching comma followed by spaces 1 or more occurrences.
(.*) ##In 4th capturing group matching everything till comma comes.
)$ ##Closing 1st capturing group till end of value.
I'm trying to implement gmail style filters in my search and I'm stuck at this regex problem. I need to capture ONE word OR two words in quotes (but without the quotation marks themselves) This is PCRE (PHP)
ie.
name:mark
desired result: 1st capture group should be mark
name:"mark"
desired result: 1st capture group should be mark
name:"mark wilson"
desired result: 1st capture group should be mark, second capture group should be wilson
name:mark wilson
desired result: 1st capture group should be mark, wilson is ignored
The closest I've gotten is name:(\w+|\"\w+(?>\"|\s([a-z.'-]+\"))) it captures example 1 perfectly, but example 2 still includes the quotes, and example 3 ends up as:
group 1: "mark wilson" (quotes included)
group 2: wilson" (quote included)
I've tried lookahead and lookbehinds but I'm not getting anywhere with those either
any help would be very appreciated. tia
1 option could be using an if/else clause which will give mark in group 2 and wilson in group 3. The first group will capture the " which can be used for the if else checking for the existence for group 1.
\w+:(")?(\w+(?:\h+(\w+))?)(?(1)")
Regex demo
If the space after the first name should not be there, you could also group that and have the values in group 3 and 4
\w+:(")?((\w+)(?:\h+(\w+))?)(?(1)")
Regex demo
You could also get either the single value between quotes or not, or capture the first or second name in a capturing group using a branch reset group
\w+:(?|"(\w+)(?:\h+(\w+))?"|(\w+))
Explanation
\w+: Match 1+ word chars
(?| Branch reset group
"(\w+) Capture group 1, match 1+ word chars
(?: Non capture group
\h+ match 1+ horizontal whitespace chars
(\w+) Capture group 2, match 1+ word chars
)? Close group and make optional
" Match "
| Or
(\w+) Capture group 1, match 1+ word chars
) Close branch reset group
Regex demo
The main point is that you cannot do that for arbitrary amount of groups, you must specify them all in the pattern at design time.
You may use a pattern like this with a branch reset group:
\w+:(?|(\w+)|"(\w+)(?:\h+(\w+))?(?:\h+(\w+))?")
See the regex demo. Add more (?:\h+(\w+))? patterns at the end to support up to N amount of optional words.
Details
\w+: - 1+ word chars and then a :
(?|(\w+)|"(\w+)(?:\h+(\w+))?(?:\h+(\w+))?") - a branch reset group where groups share the same IDs:
(\w+) - Group 1: one or more word chars
| - or
"(\w+)(?:\h+(\w+))?(?:\h+(\w+))?" -
" - a " char
(\w+) - Group 1: one or more word chars
(?:\h+(\w+))? - an optional occurrence of a sequences:
\h+ - 1 or more horizontal whitespaces
(\w+) - Group 2: one or more word chars
(?:\h+(\w+))?" - ibid, but with Group 3, etc.
text = "Happy 4/20 from the team! 13/10 congrats..after so many contents"
I want to fetch only 13/10 which is the rating. I have written regex
(\d+\.\d+|\d+)/(((?=10)10)|([1-9]\d+))
but it fetches the first one(4/20).
Is this possible to achieve using regex?
In this part of your pattern (?=10)10 you can omit the positive lookahead because that says if what is on the right is 10, then match 10. This part [1-9]\d+ matches 10 and above so 10 is already in the range.
You could use a capturing group with a quantifier {2} to repeat that group.
Your pattern can also be written as \d+(?:\.\d+)?/[1-9]\d+)
To get the second group, you might use:
^(?:.*?(\d+(?:\.\d+)?/[1-9]\d+)){2}
^ Start of the string
(?: Non capturing group
.*? Match any char non greedy
( Capturing group
\d+(?:\.\d+)? Match 1+ digits, optionally match a dot and 1+ digits
/ Match /
[1-9]\d+ Match 10 and above
) Close capturing group
){2} Close non capturing group and repeat 2 times
Regex demo