I am a newbie to Regex and require help with the following:
I have strings like - B - Comp-Band Disk,C - Check Oncoming Private,D - DL Procurement Outer. Is there a Regex expression which I could use to change string to B,C,D?
You can use
(?:(?:^|,)(\w))
Regex Explanation
(?: Non-capturing group
(?: Non-capturing group
^|, Match start of the string or ,
) Close non-capturing group
( Capturing group
\w Match any word character
) Close group
) Close non-capturing group
See the demo
Related
I'm using Overpass API's regex. Unsure which flavour it uses.
I'm wishing to capture these strings:
"Footpath"
"Public Footpath"
"Footpath No. 27001"
"Public Footpath No. 125"
"Footpath #424"
"Public Footpath #5"
This fails to return the first two options.
^(Public)?Footpath (No\. |#)?[0-9]
How do I make the 'No./# optional?
I've tried variations on wrapping them in brackets, but to no avail eg.
^(Public)?Footpath ((No\. |#)?[0-9])?
I'm afraid I'm out of my depth.
You may use this regex with multiple optional non-capturing groups:
^(?:Public )?Footpath(?: No\.)?(?: #?[0-9]+)?$
RegEx Demo
RegEx Details:
^: Start
(?:Public )?: Match Public in an optional non-capturing group
Footpath: Match Footpath
(?: No\.)?: Match No\. in an optional non-capturing group
(?: #?[0-9]+)?: Match space followed by optional # and 1+ digits in an optional non-capturing group
$: End
I would like to capture the date and time from the text below.
!----------------------------------------------
! 16/Oct/2020 10:11:14 12/Nov/2020 11:21:32
!----------------------------------------------
! 17/Oct/2020 10:11:14
!----------------------------------------------
! 18/Oct/2020 11:00:00 21/Oct/2020 12:00:00
!----------------------------------------------
My regex query:
(?P<StartDate>(?<=!\s)[^\s]+)\s+(?P<StartTime>[^\s]+)\s*(?P<EndDate>[^\s]+)\s+(?P<EndTime>[^\s]+)
However, for the second row it is capturing the exclamation mark and hyphen as well. How can I uncapture those things?
Use the following regex. This is more specific than just the generic [^\s].
Side note, [^\s] can be replaced with \S.
You can make the end date and time optional by wrapping them in a non-capturing group (?:) and then adding a question mark after it to make that group optional: (?:)?
Regex
(?<=!\s)(?P<StartDate>[0-3]?[0-9]\/[A-Za-z]+\/\d+)\s+(?P<StartTime>[0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9])\s+(?:(?P<EndDate>[0-3]?[0-9]\/[A-Za-z]+\/\d+)\s+(?P<EndTime>[0-2]?[0-9]:[0-5]?[0-9]:[0-5]?[0-9]))?
Formatted
(?<=!\s) # Look behind if starts with "! "
(?P<StartDate>
[0-3]?[0-9]
\/
[A-Za-z]+
\/
\d+
)
\s+
(?P<StartTime>
[0-2]?[0-9]
:
[0-5]?[0-9]
:
[0-5]?[0-9]
)
\s+
(?: # non capturing group
(?P<EndDate>
[0-3]?[0-9]
\/
[A-Za-z]+
\/
\d+
)
\s+
(?P<EndTime>
[0-2]?[0-9]
:
[0-5]?[0-9]
:
[0-5]?[0-9]
)
)? # Make this group optional
Demo
https://regex101.com/r/Ky7g45/1
Cons
This will also match invalid dates from 32 - 39 and time hours from 24 - 29. If that matters, you'll need to add more regex with the | operator.
You should make your capture groups optional (at least the end time/date).
(?P<StartDate>(?<=!\s)[^\s]+)\s+(?P<StartTime>[^\s]+)\s*(?P<EndDate>[^!\s]+)?\s+(?P<EndTime>[^!\s]+)?
Here I make the EndDate and EndTime capture groups optional and also explicitely exclude exclamation marks (this is another avenue to explore, making the capture groups more specific to match only a date/time and not any non-whitespace characters).
For example, the dates can be matched with
[0-9]{2}\/[A-Za-z]{3}\/[0-9]{4}
and the times with
[0-9]{2}:[0-9]{2}:[0-9]{2}
A pretty similar RegExp of yours
with a lookbehind (?<=) and an optional non-capturing group (?:)?:
(?<=!\s)(?P<StartDate>\S+)\s+(?P<StartTime>\S+)(?:\s+(?P<EndDate>\S+)\s+(?P<EndTime>\S+))?
Description and example at: Regex101.com
I know it's been asked many many times. I tried my best but the result wasn't perfect.
Regex
/(\(\s*["[^']*]*)(.*\/logo\.png.*?)(["[^']*]*\s*\))/gmi
Regex101 Link: https://regex101.com/r/0f8Q08/1
It should capture all separately.
(../asdasd/dasdas/logo.png)
(../asdasd/dasdas/logo.png)
( '../logo.png' )
Right now it's capturing as a whole.
(../asdasd/dasdas/logo.png) (../asdasd/dasdas/logo.png) ( '../logo.png' )
What I need is, the regex to stop after the first closing bracket ) match.
You can use
(\(\s*(["']?))([^"')]*\/logo\.png[^"')]*)(\2\s*\))
See the regex demo.
Details
(\(\s*(["']?)) - Group 1: (, any zero or more whitespaces, and then Group 2 capturing either a ' or a " optionally
([^"')]*\/logo\.png[^"')]*) - Group 3: any zero or more chars other than ", ' and ), then a /logo.png string, and then again any zero or more chars other than ", ' and )
(\2\s*\)) - Group 4: the same value as in Group 2, zero or more whitespaces, and a ) char.
The issue in your pattern is that the .* matches too much. After the opening parenthesis, you should exclude matching the ( and ) to overmatch the separate parts.
You don't need all those capture groups if you want to match the parts with parenthesis as a whole.
You can use 1 capture group, where the group would be a backreference matching the same optional closing quote.
\(\s*(["']?)[^()'"]*\/logo\.png[^()'"]*\1\s*\)
Regex demo
If you also want the matches without the matching quotes:
\(\s*["']?[^()'"]*\/logo\.png[^()'"]*["']?\s*\)
Regex demo
If you want to use regex you can make the change from .* to [^)] so you stay between parenthesis
(\(\s*["[^']*]*)([^)]*\/logo\.png.*?)(["[^']*]*\s*\))
regex101
I'm trying the create a regex to catch my url and his, optionnals, groups. The regex works fine if the url is complete. The optionnals groups are not optionnals at all.
Regex :
\/(.+)(?:\/(.+))(?:(?:\?(.+)))
Urls to catch :
/taxi
/taxi/lyon
/taxi/lyon?coordinates=7542
https://regex101.com/r/NKFkwq/4/
As you can see, the third line is catched. But i'd like the first and second too.
I thought the ?: will be enought to do that, but i missed something...
Thanks a lot for your help !
Cheers
EDIT and answer
Thanks in the comments for helping me. Here the great regex (the one i expected) : https://regex101.com/r/NKFkwq/8
Indeed ?: is about ignoring a match, not made him optionnal.
Your pattern consists of capturing and non capturing groups. The (?: denotes a non capturing group.
If you want to match all 3 lines, you could use match the part starting from the first forward slash and make the part starting from the second forward slash optional.
^/[^\s/]+(?:/[^\s/]+)?$
^ Start of string
/[^\s/]+ Match / and match 1+ times any char except a whitespace or /
(?: Non capturing group
/[^\s/]+ Match / and match 1+ times any char except a whitespace or /
)? Close non capturing group and make it optional
$ End of string
Regex demo
If you want to have capturing groups, but don't want to match /taxi?coordinates=7542 you could nest the groups and make them optional as well.
^/\w+(/\w+(\?\S*)?)?$
^ Start of string
/\w+ Match / and 1+ word chars
( Capture group 1
/\w+ Match / and 1+ word chars
( Capture group 2
\?\S* Match ? and 0+ times a non whitespace char
)? Close group 2
)? Close group 1
$ End of string
Regex demo
I have to collect two informantion from a text using regex. The name and the database and relate then in one table. But a can only collect then individually.
This is an example, i have many blocks of these, and two of then don't have a database value, these i need to ingnore
[SCD] {I need the name between []}
Driver=/opt/pcenter/pc961/ODBC7.1/lib/DWmsss27.so
Description=
Database=scd {I need the value after Defaut|Database}
Address=#######
LogonID=######
Password=######
QuoteId=No
AnsiNPW=No
ApplicationsUsingThreads=1
The regex to find the name is:
(?<=\[)(.*)(?=\])
The regex to find the value after database is
(?<=Defaut|Database=)(.*)
How can i combine both of then into onde regex ?
To match both values you could use 2 capturing groups instead and use a repeating pattern and a negative lookahead to check if a line do not start with Default of Database until the line does.
\[([^]]+)\](?:\r?\n(?!Default|Database).*)*\r?\n(?:Default|Database)=(\S+)
About the pattern
\[ Match [
( Capture group 1
[^]]+ match 1+ times not ]
) Close group 1
\] Match ]
(?: Non capturing group
\r?\n Match newline,
(?! Negative lookahead, assert what is directly on the right is not
Default|Database Match one of the options
).* Close negative lookahead and match any char except a newline 0+ times
)* Close non capturing group and repeat 0+ times
\r?\n(?:Default|Database)= Match newline, any of the options and =
(\S+) Capturing group 2, match 1+ times a non whitespace char (or use (.+) to match any char 1+ times)
regexstorm demo