I'm new with regex and really need some help here. I'm trying to create a regex that finds everything before the first space and open parenthesis in the example below. Basically, I'm trying to just keep the country name or names and exclude everything after.
Falkland Islands (Malvinas)
I tried this but it isn't working:
(\w+)(?=[\s+(\w+\s+])
Use
^.*?(?=\s*\()
See proof here.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\( '('
--------------------------------------------------------------------------------
) end of look-ahead
Related
Assume I have this text:
blah blah Bob Loblaw Law blah
keep1 { i want this } blop
blah blob keep2 { and
this too } blaw blat
etc...
And I want to end up with
keep1 { i want this }
keep2 { and
this too }
or perhaps:
keep1 { i want this }
keep2 { and this too }
I haven't figured out how to get Atom's regexp find/replace mechanism to discard everything across multiple lines outside of a specific matching string. Hints?
update:
Of the many things I've tried, this gets me closest:
[\S\s]+?(keep\d\s+\{[\S\s]+?\})
which results in:
keep1 { i want this }
keep2 { and
this too }
blaw blat
etc...
This is probably good enough -- I can edit the trailing shards -- but it would be useful to know how to trim those as well.
You may use this simple regex replace in Atom for this task:
\b(keep\d+\s*{[^}]*})|.+?
Replace it with: $1
RegEx Demo
RegEx Details:
\b: Word boundary
(keep\d+\s*{[^}]*}): In capture group #1 match a string that starts with keep followed by 1+ digits followed by 0+ whitespaces followed by any text that is inside {...} spanning across the lines as well. This assumes { and } are balanced and there is no escaping of { and }.
|: OR
.+?: Lazily match 1+ of anything
PS: If you want to remove leading line break then use:
\n?\b(keep\d+\s*{[^}]*})|.+?
Atom Editor Demo
Before replacement:
After replacement:
Use
[\s\S]*?(keep\d\s+\{[^{}]*\})|(?:(?!keep\d\s+\{[^{}]*\})[\s\S])+$
See proof.
EXPLANATION
--------------------------------------------------------------------------------
[\s\S]*? any character of: whitespace (\n, \r, \t,
\f, and " "), non-whitespace (all but \n,
\r, \t, \f, and " ") (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
keep 'keep'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\{ '{'
--------------------------------------------------------------------------------
[^{}]* any character except: '{', '}' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\} '}'
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
keep 'keep'
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ")
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\{ '{'
--------------------------------------------------------------------------------
[^{}]* any character except: '{', '}' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\} '}'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[\s\S] any character of: whitespace (\n, \r,
\t, \f, and " "), non-whitespace (all
but \n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
I'm trying to write a regex for matching user input that will be turned into italic format using markdown.
In the string i need to find the following pattern: an asterisk followed by any kind of non-whitespace character and ending with any kind of non-whitespace character followed by an asterisk.
So basically: substring *substring substring substring* substring should spit out *substring substring substring*.
So far I came up only with /\*(?:(?!\*).)+\*/, which matches everything between two asterisks, but it doesn't take into consideration whether the substring between asterisks starts or end with whitespace - which it shouldn't.
Thank you for your input! :)
Use
\*(?![*\s])(?:[^*]*[^*\s])?\*
See regex proof.
EXPLANATION
--------------------------------------------------------------------------------
\* '*'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
[*\s] any character of: '*', whitespace (\n,
\r, \t, \f, and " ")
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
[^*]* any character except: '*' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
[^*\s] any character except: '*', whitespace
(\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
\* '*'
I have the following string
This is some testing dbo.GetPersonData(this_.PersonId,'Date contract recieved from the client') cs.dbo.Person 'this is test'
I want it generic and obtain the following result
This
is
some
testing
dbo.GetPersonData(This_.PersonId,
'DateContract received from the client'
)
cs.dbo.person
'this is test'
What I am trying to achieve here is to split by spaces apart of anything between single quotation
I have tried using
'(.*?)'|\S+
However, it is ignoring: <'Date contract recieved from the client'>
Result
I have tried using
'(.*?)'|\S+
Your try was not so bad, we just have to exclude the quote from the second alternative:
'.*?'|[^\s']+
regex101
\S matches any non-whitespace, and ', too.
Use
'([^']*)'|[^\s']+
Or, if you have PCRE:
(?|'([^']*)'|([^\s']+))
See proof.
Explanation
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
' '\''
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
[^\s']+ any character except: whitespace (\n, \r,
\t, \f, and " "), ''' (1 or more times
(matching the most amount possible))
You should use the following template:
(\n.*?)*)\n\s*\n
I am scanning a QR code and need a script to replace the commas with a ( \t)
My results are:
820-20171-002, ,Nov 24, 2020,,,13,283.40,,Mike Shmow
My problem is - I don't want a comma after the date. Right now I have the following - which does work to replace commas with a tab.
decodeResults[0].content.replace(/,/g, "\t");
I am trying to replace the /,/g with an expression to replace all commas except for the 3rd occurrence.
Use
.replace(/(?<!\b[a-zA-Z]{3}\s+\d{1,2}(?=,\s*\d{4})),/g, '\t')
See proof
Explanation
--------------------------------------------------------------------------------
(?<! Negative lookbehind start, fail if pattern matches
--------------------------------------------------------------------------------
\b the boundary between a word char (\w)
and something that is not a word char
--------------------------------------------------------------------------------
[a-zA-Z]{3} any character of: 'a' to 'z', 'A' to 'Z'
(3 times)
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\d{1,2} digits (0-9) (between 1 and 2 times
(matching the most amount possible))
--------------------------------------------------------------------------------
(?= look ahead to see if there is:
--------------------------------------------------------------------------------
, ','
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
) end of negative lookbehind
--------------------------------------------------------------------------------
, ','
Correct code:
"key1=val1;key2=val2;key3=val3" -- Correct as each pair is having ";" at the end except the last pair
Incorrect code:
"key1=val1;key2=val2; key3=val3;" -- Invalid as last pair is having ";" at the end
"key1=val1;;;key2=val2;;;key3=val3" -- Invalid as there are multiple ";" in the middle
I got the regex below from some old link in stackoverflow, but it is not working in the above case:
^(?:\s*\w+\s*=\s*[^;]*;)+$
You might use
^\w+\s*=\s*\w+(?:;\s*\w+\s*=\s*\w+)*$
Explanation
^ Start of string
\w+\s*=\s*\w+ Match 1+ word chars, = and 1+ word chars with optional whitespace chars
(?: Non capture group
;\s*\w+\s*=\s*\w+ Match ; and the same patter as mentioned above
)* Close the group and repeat 0+ times
$ End of string
Regex demo
With the doubled backslashes
^\\w+\\s*=\\s*\\w+(?:;\\s*\\w+\\s*=\\s*\\w+)*$
Also, a shorter one:
^(?:\s*\w+\s*=\s*\w+(?:;(?!\s*$)|\s*$))+\s*$
See proof
Explanation
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
= '='
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end
of the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ")
(0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
)+ end of grouping
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
Try below:
.*\w;\w.*\w;\w.*[^;]$
Test here
Explanation:
.* --> matches any character
\w --> matches any word character
[^;]$ --> Will exclude any line ending with ;
I find things like this much easier without regex. For eample with JavaScript:
function isValid(string) {
return string.split(/;/).map(e => e.split(/=/)).every(e => e.length === 2);
}