I have a string that I've matched with regex but that same string is commented out with a # sign in front and regex keep matching it which I do not want.
My Regex
BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
String
BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213)-> only match this
#BLTY:ENCQ0000:SERVER:TEMP.PPMQ8FE.Y.323/TCP/gtg23.dev.pmt.com(3213) -> I dont want to match this
Tried
^[BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)]
^BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
(?!#)BLTY:\w{8}:\w{8}:\w{5}\.\w{7}\.\w{1}\.\w{3}\/\w{3}\/.*\(\w{4}\)
Also if there's a less verbose/optimized way of writing this regex Im open to hear
No need using lookaheads:
^BLTY(?::\w+){3}(?:\.\w+){3}/.*\(\d+\)$
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
BLTY 'BLTY'
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
/ '/'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\) ')'
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Related
I want to replace map["someKey"] as double by (map["someKey"] as num).toDouble(), but I just can't get the regex working correctly. Can someone who knows more about regex tell me what to put in Find and Replace in VSCode?
I've tried ([a-zA-Z0-9]*) as double and replaced this by ($1 as num).toDouble(), but this doesn't work.
Use
(\w+\["[^"]+"\]) as double
See regex proof. Replace with ($1 as num).toDouble().
EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\[ '['
--------------------------------------------------------------------------------
" '"'
--------------------------------------------------------------------------------
[^"]+ any character except: '"' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
"\] '"]'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
as double ' as double'
I am trying to match a sequence of four numbers that are separated by pipes in a string. The numbers may be negative, float, or double digits, for example:
13|5|-1|3 or 5|5|0|3 or 13|4|1.5|1
The string may also contain additional numbers and words; a full example looks like so:
SOME STRING CONTENT 13|5|-1|3 MORE 1.6 CONTENT HERE
How could I identify those numbers between and to the left/right of the pipes using regex?
I have tried [\d\-.\|] which matches all digits, decimals, pipes, and negative signs but also find it matches the additional number/decimal content in the string. Any help on just selecting that one section would be appreciated!
You can use
-?\b\d+(?:\.\d+)?(?:\|\-?\d+(?:\.\d+)?){3}\b
The pattern matches:
-? Match an optional -
\b A word boundary to prevent a partial match
\d+(?:\.\d+)? Match 1+ digits with an optional decimal part
(?:\|\-?\d+(?:\.\d+)?){3} Repeat 3 times the same as previous part preceded by a pipe
\b A word boundary
Regex demo
As well use
(?<!\S)-?\d*\.?\d+(?:\|-?\d*\.?\d+){3}(?!\S)
See proof.
EXPLANATION
--------------------------------------------------------------------------------
(?<! look behind to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-behind
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (3 times):
--------------------------------------------------------------------------------
\| '|'
--------------------------------------------------------------------------------
-? '-' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
\.? '.' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
){3} end of grouping
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-ahead
My current regex:
(\d.{17})[^#]*(\D+)(\d+)gr(\d+)
In group 2, they are still having the hashtag, I want to remove it from there. What should I change from my current regex?
201223E0MWJPJD2230#AdeSaputra290gr99000
2101023CNV6TT1109J#Fefe430gr142000
2101183EDTFPSA0128#Jessica500gr112000
201221E2QKWRY11413#EssyYosita880gr233500
2101123G9XQ7R41705#Meily1120gr329000
201228ECEWTJT50859#WidyaNatali1720gr457230
201227EEBX1K9K1020#Excelio112gr58900
2101112N4YNFB12016#DebyNath520gr156220
2101072R8A0QB22347#AlycieHandoTan700gr85000
Output:
group 1: 201223E0MWJPJD2230
group 2: #AdeSaputra
group 3: 290
group 4: 99000
remove [^] from your regex
(\d.{17})#*(\D+)(\d+)gr(\d+)
see this
\D matches any non-digits and it matches # in your input.
Add # to the pattern instead of the opposite [^#] so as to match it.
Use
^(\d.{17})#(\D+)(\d+)gr(\d+)$
See proof it works. Adding anchors to match entire strings.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
.{17} any character except \n (17 times)
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
# '#'
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
\D+ non-digits (all but 0-9) (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
( group and capture to \3:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \3
--------------------------------------------------------------------------------
gr 'gr'
--------------------------------------------------------------------------------
( group and capture to \4:
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
) end of \4
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
I have been using this:
~^\/student-accommodation\/(?:[^\/]+?)\/([^\/]+)\/$
to match for URLs like
/student-accommodation/manchester/ropemaker-court-manchester/
But now I need to edit this regex so it also matches for URLs like the below. All these new URLs will follow the same pattern and add a string that starts with #utm-source. Importantly they won't have another / in them.
/student-accommodation/manchester/ropemaker-court-manchester/#utm_source=afs&utm_medium=email&utm_campaign=ropemakercourt_afs_dec20
But then I don't want the regex to match for URLs like the below:
/student-accommodation/manchester/ropemaker-court-manchester/en-suite/
Can anyone help? I am a novice at regex! Thanks
Use
^\/student-accommodation\/[^\/]+\/([^\/]+)\/(?:#utm_source.*)?$
See proof
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
student- 'student-accommodation'
accommodation
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^\/]+ any character except: '\/' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
#utm_source '#utm_source'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
I'd like to match positively strings like "10a3b4c", "10a", "5b4c", "3a6c", but not match "2c1b" (because the letters aren't in alphabetical order) or the empty string.
Attempt: (\d+a)?(\d+b)?(\d+c)?
Problem: Matches the empty string. It falsely matches "".
Attempt: (\d+[abc]){1,3}
Problem: Doesn't enforce the a, b, c order. It falsely matches "2c1b"
How can this restriction be expressed as a regex?
Use
^(?!$)(\d+a)?(\d+b)?(\d+c)?$
See proof
Explanation
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
( group and capture to \1 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
a 'a'
--------------------------------------------------------------------------------
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
( group and capture to \2 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
b 'b'
--------------------------------------------------------------------------------
)? end of \2 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \2)
--------------------------------------------------------------------------------
( group and capture to \3 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
c 'c'
--------------------------------------------------------------------------------
)? end of \3 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \3)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
This seems to work (see it on regex101)
\b(?=[\dabc]+\b)(\d+a)?(\d+b)?(\d+c)?\b