Need a regular expression to match a pattern - regex

I need a regular expression to match below pattern
Word1 OR Word2 OR Word3 OR......
basically this is a string which contains words split by OR

You can do:
(\w+)(?=(?:\s+OR)|(?:\s*$))
Demo

The following will match based on what you've given:
^\w+(?: OR \w+)*$
^ assert position at start of the string
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?: OR \w+)* Non-capturing group
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
_OR_ matches the characters _OR_ literally (case sensitive)
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
NOTE: I used _OR_ to show the spaces around OR in the quotation as the whitespace was ignored.
Link to Regex101

Do something like this and access the second group using \2:
((\w+)\sOR)*
Try playing with the link below, read the explanation and you'll understand how it works:
https://regex101.com/r/vY6mA7/1

Related

Extract phone number from complicated string

Assume we got address book with some unformatted data, like:
+1 (4542) 114214 111#111.org d#ghhg.com,,,,
+1 (2342) 114234 ert#nhy.sdfr.domain.org; 1#kjk.eiu.1
+7 (101) 111-222-11 abc#ert.com, def#sdf.org
+1 (102) 123532-2 some#mail.ru
+44 (301) 123 23 45 7zip#site.edu; ret#ghjj.org
I made attempts to write regex for this:
/+\d+\s(\d+)\s\d+[\d+\s | \d+ -]+/g
But i have no idea how to exclude numbers before alphabetical characters. Probably this is not even a partial solution.
Edit #1: I'm overwhelmed by all working solutions provided, many thanks to everyone. If possible, i would be grateful if you added at least some reference/explanation how to write such complicated regex.
Without knowing where you are using this regex I'd recommend using a negative lookahead.
^[+\d() -]+(?![\w#])
Demo: https://regex101.com/r/rQ6fK4/1
If you want to capture the phone number use:
^([+\d() -]+)(?![\w#])
It will be in $1 or \1, (depending on where you are using this).
That may be one of few cases, where you need a possessive quantifier.
My attempt:
\s*(\+?(\d+)\s*\(\d+\)\s+([- \d+]++(?!\#)|\d+))
The part [- \d+]++(?!\#) will stop the matching if a "#" is following. Therefore it excludes the e-mail addresses.
The phone numbers are now stored in group 1.
Edit:
Yes, the last input line doesn't match correctley. Maybe it's easier to extract the e-mail addresses with the following regex, so the phone numbers are left (and some commas, but they should be a problem to extract as well):
\s[^\# ]+\#[-\w.]+\.\w+
Here's my solution (on regex101):
\+\d+\s+\(\d+\)\s+[- \d]+(?= )
It ensures the last group of spaces, digits, and/or dashes ([- \d]+) is followed by a space ((?= )).
It cleanly captures all of your examples, without trailing spaces, and without including any part of the email addresses.
I wouldn't even try parsing the phone number.
You have a phone number, separated with a space character from one or more email addresses, which are separated by commas or semicolons. An email address always contains a #.
Find the first #. If there is none then the phone number is the trimmed string. If there is an # then find the last space before the #. The phone number is the everything up to that space, trimmed. If there is no space before the # then you have no phone number.
With phone numbers removed, you can find the emails by splitting the string around "," or ";", trimming the strings, and throwing away what doesn't contain an #.
Then find a decent number to handle phone numbers, if you need to do that beyond recording what the phone number is.
You can use see demo:
(?<phone>\+\d{1,2}\s\(\d{3,4}\)\s(?:[\d- ]+\d)(?=\s))
\s+(?<email>.*?#.*?)(?=[\s;,]|$).*?
\s+(?<email2>[\w]*?#.*?)?(?=[\s;,]|$)
Which produce:
MATCH 1
phone [4-20] `+1 (4542) 114214`
email [21-32] `111#111.org`
email2 [33-43] `d#ghhg.com`
MATCH 2
phone [52-68] `+1 (2342) 114234`
email [69-92] `ert#nhy.sdfr.domain.org`
email2 [94-105] `1#kjk.eiu.1`
MATCH 3
phone [110-129] `+7 (101) 111-222-11`
email [130-141] `abc#ert.com`
email2 [143-154] `def#sdf.org`
MATCH 4
phone [159-176] `+1 (102) 123532-2`
email [177-189] `some#mail.ru`
MATCH 5
phone [194-213] `+44 (301) 123 23 45`
email [214-227] `7zip#site.edu`
email2 [229-241] `ret#ghjj.org`
Explanation:
(?<phone>\+\d{1,2}\s\(\d{3,4}\)\s(?:[\d- ]+\d)(?=\s)) Named capturing group phone
\+ matches the character + literally
\d{1,2} match a digit [0-9]
Quantifier: {1,2} Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
\s match any white space character [\r\n\t\f ]
\( matches the character ( literally
\d{3,4} match a digit [0-9]
Quantifier: {3,4} Between 3 and 4 times, as many times as possible, giving back as needed [greedy]
\) matches the character ) literally
\s match any white space character [\r\n\t\f ]
(?:[\d- ]+\d) Non-capturing group
[\d- ]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
- a single character in the list - literally
\d match a digit [0-9]
(?=\s) Positive Lookahead - Assert that the regex below can be matched
\s match any white space character [\r\n\t\f ]
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?<email>.*?#.*?) Named capturing group email
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
# matches the character # literally
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
(?=[\s;,]|$) Positive Lookahead - Assert that the regex below can be matched
1st Alternative: [\s;,]
[\s;,] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
;, a single character in the list ;, literally
2nd Alternative: $
$ assert position at end of a line
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
\s+ match any white space character [\r\n\t\f ]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
(?<email2>[\w]*?#.*?)? Named capturing group email2
Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
[\w]*? match a single character present in the list below
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
\w match any word character [a-zA-Z0-9_]
# matches the character # literally
.*? matches any character (except newline)
Quantifier: *? Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
(?=[\s;,]|$) Positive Lookahead - Assert that the regex below can be matched
1st Alternative: [\s;,]
[\s;,] match a single character present in the list below
\s match any white space character [\r\n\t\f ]
;, a single character in the list ;, literally
2nd Alternative: $
$ assert position at end of a line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])
x modifier: extended. Spaces and text after a # in the pattern are ignored

Use RegEx to match URL [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm writing a regex to pull a URL out of an auto-generated email from my monitoring system. For example:
https://mon.contoso.com/mon/call.py?fn=edit&num=1389896156
I need a regex to match:
https://mon.contoso.com/mon/call.py?fn=edit&num=XXXXXXXXX
whereby the "x"'s always change. I run into an issue with the "?". The point of this is to append the URL to a field in JIRA.
Pattern p = new Pattern("https://mon.contoso.com/mon/call.py?fn=edit&num=(\d+)")
Matcher m = p.matcher(inputEmail);
return m.matches() ? m.group(1) : "";
This returns num if it is numeric, otherwise you might want to use \w instead of \d. If you want the whole URL, remove the group() parameter.
You don't indicate what language you're working in.
In Python and JavaScript, this regex will identify a variety of URLs:
/\[[^\]\n]+\](?:\([^\)\n]+\)|\[[^\]\n]+\])|(?:\/\w+\/|.:\\|\w*:\/\/|\.+\/[./\w\d]+|(?:\w+\.\w+){2,})[./\w\d:/?#\[\]#!$&'()*+,;=\-~%]*/gi
You can refer to this regex101 test for examples of the regex in use.
Explanation:
/\[[^\]\n]+\](?:\([^\)\n]+\)|\[[^\]\n]+\])|(?:\/\w+\/|.:\\|\w*:\/\/|\.+\/[./\w\d]+|(?:\w+\.\w+){2,})[./\w\d:/?#\[\]#!$&'()*+,;=\-~%]*/gi
1st Alternative: \[[^\]\n]+\](?:\([^\)\n]+\)|\[[^\]\n]+\])
\[ matches the character [ literally
[^\]\n]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\] matches the character ] literally
\n matches a line-feed (newline) character (ASCII 10)
\] matches the character ] literally
(?:\([^\)\n]+\)|\[[^\]\n]+\]) Non-capturing group
1st Alternative: \([^\)\n]+\)
\( matches the character ( literally
[^\)\n]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\) matches the character ) literally
\n matches a line-feed (newline) character (ASCII 10)
\) matches the character ) literally
2nd Alternative: \[[^\]\n]+\]
\[ matches the character [ literally
[^\]\n]+ match a single character not present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\] matches the character ] literally
\n matches a line-feed (newline) character (ASCII 10)
\] matches the character ] literally
2nd Alternative: (?:\/\w+\/|.:\\|\w*:\/\/|\.+\/[./\w\d]+|(?:\w+\.\w+){2,})[./\w\d:/?#\[\]#!$&'()*+,;=\-~%]*
(?:\/\w+\/|.:\\|\w*:\/\/|\.+\/[./\w\d]+|(?:\w+\.\w+){2,}) Non-capturing group
1st Alternative: \/\w+\/
\/ matches the character / literally
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\/ matches the character / literally
2nd Alternative: .:\\
. matches any character (except newline)
: matches the character : literally
\\ matches the character \ literally
3rd Alternative: \w*:\/\/
\w* match any word character [a-zA-Z0-9_]
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
: matches the character : literally
\/ matches the character / literally
\/ matches the character / literally
4th Alternative: \.+\/[./\w\d]+
\.+ matches the character . literally
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\/ matches the character / literally
[./\w\d]+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
./ a single character in the list ./ literally
\w match any word character [a-zA-Z0-9_]
\d match a digit [0-9]
5th Alternative: (?:\w+\.\w+){2,}
(?:\w+\.\w+){2,} Non-capturing group
Quantifier: {2,} Between 2 and unlimited times, as many times as possible, giving back as needed [greedy]
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\. matches the character . literally
\w+ match any word character [a-zA-Z0-9_]
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
[./\w\d:/?#\[\]#!$&'()*+,;=\-~%]* match a single character present in the list below
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
./ a single character in the list ./ literally
\w match any word character [a-zA-Z0-9_]
\d match a digit [0-9]
:/?# a single character in the list :/?# literally
\[ matches the character [ literally
\] matches the character ] literally
#!$&'()*+,;= a single character in the list #!$&'()*+,;= literally (case insensitive)
\- matches the character - literally
~% a single character in the list ~% literally
g modifier: global. All matches (don't return on first match)
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

How to regex just the second half of firsthalf.secondhalf():

Having trouble getting the regex to work for this. I want to basically just recognize the second half of something like this: firsthalf.secondhalf(): as a function. So in the example above just the .secondhalf(): would be recognized as unique and different color than the firsthalf.
I've tried, but to no avail:
<regex>(\w*()\b)</regex>
Try following:
(\w*\(\):)
Debuggex Demo
\w*\(\)\:$
\w* match any word character [a-zA-Z0-9_]
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\( matches the character ( literally
\) matches the character ) literally
\: matches the character : literally
$ assert position at end of the string

Javascript transformation

Is there any simple way to transform:
"<A[hello|home]>"
to:
"hello|home"
Thanks!
Apart from the clever advice in the comments to simply remove certain characters, if you are unable to remove these characters because they are present elsewhere in the text and do want to match that format, here is a way to do it with regex:
Search: <\w+\[([^|]*\|[^\]]*)\]>
Replace: \1 or $1 depending on editor or regex engine.
See the Substitution pane at the bottom of the demo.
Explanation
<\w+\[([^|]*\|[^\]]*)\]>
Match the character “<” literally <
Match a single character that is a “word character” (Unicode; any letter or ideograph, digit, connector punctuation) \w+
Between one and unlimited times, as many times as possible, giving back as needed (greedy) +
Match the character “[” literally \[
Match the regex below and capture its match into backreference number 1 ([^|]*\|[^\]]*)
Match any character that is NOT a “|” [^|]*
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
Match the character “|” literally \|
Match any character that is NOT a “]” [^\]]*
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) *
Match the character “]” literally \]
Match the character “>” literally >
\1
Insert the backslash character \
Insert the character “1” literally 1

Regex for 2 items but with one exclusion

I am building a RegEx that needs to find lines that have either:
DateTime.Now
or
Date.Now
But cannot have the literal "SystemDateTime" on the same line.
I started with this (DateTime\.Now|Date\.Now) but now I am stuck with where to put the "SystemDateTime"
Use this. Assuming you are not using /s modifier(or DOTALL) which takes newline characters under the dot(.)
(?!.*SystemDateTime)(DateTime\.Now|Date\.Now)
(?!.*SystemDateTime) means there is no SystemDateTime in front.
You could use negative lookahead like this:
(?!.*SystemDateTime)\bDate(?:Time)?\.Now\b
/(?!.*SystemDateTime)Date(?:Time)?\.Now/
DEMO
EXPLANATION:
Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!.*SystemDateTime)»
Match any single character that is not a line break character «.*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the characters “SystemDateTime” literally «SystemDateTime»
Match the characters “Date” literally «Date»
Match the regular expression below «(?:Time)?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match the characters “Time” literally «Time»
Match the character “.” literally «\.»
Match the characters “Now” literally «Now»