Regex match specific word with dollar sign? - regex

I have sentences like:
$COIN has a new price target increase to $400
I only want to match $COIN with regex, I am wondering how to do this?
If I do something like .*\\$.* it also matches $400. I would just like to match the $SOMEWORDNOSPACE only. Is that possible?
Thanks

If everything after $ until the end of the word is a capital letter: \$[A-Z]+
This will match the $ (\$), and then match between 1 and infinity capital letters [A-Z]+. The match stops when a character doesn't fit in the A-Z range, so \b is unnecessary. If the match can't start in the middle of the sentence you could start with \B so it starts matching on a switch of a word character to the dollar sign, in that case the regex would be \B\$[A-Z]+

Use
(?<!\S)\$[A-Z]+(?!\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
--------------------------------------------------------------------------------
\$ '$'
--------------------------------------------------------------------------------
[A-Z]+ any character of: 'A' to 'Z' (1 or more
times (matching the most amount possible))
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
\S non-whitespace (all but \n, \r, \t, \f,
and " ")
--------------------------------------------------------------------------------
) end of look-ahead

Related

Regex for matching predefined rules for italic text formatting

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
--------------------------------------------------------------------------------
\* '*'

How do I use regex to add text in between a set of quotes?

I have a regular expression to match a string in my configuration file.
/\s+apiEndpoint:\n\s+''/gm
This regex matches the following field in my JavaScript file.
apiEndpoint:
'';
How do I extend this regex so that it inserts text https://localhost:6000 between the set of single quotes?
apiEndpoint:
'https://localhost:6000';
Use this to add:
(\s+apiEndpoint:\n\s+)''
Use this to update or add:
(\s+apiEndpoint:\n\s+)'[^']*'
Replace with $1'https://localhost:6000'.
See proof.
EXPLANATION
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
apiEndpoint: 'apiEndpoint:'
--------------------------------------------------------------------------------
\n '\n' (newline)
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of \1
--------------------------------------------------------------------------------
'' '\'\''
--------------------------------------------------------------------------------
[^']* any character except: ''' (0 or more times
(matching the most amount possible))
You can add matching between ' using negated character class starting with [^
\s+apiEndpoint:\n\s+'[^\s']*'
The pattern matches:
\s+apiEndpoint:\n\s+ Match 1+ whitespace chars, apiEndpoint: a newline and 1+ whitespace chars
' Match '
[^\s']* match 0+ times any char except a whitspace char or '
' Match closing '
» Regex demo
Or if you want to allow whitespace chars and excaped \' in between:
\s+apiEndpoint:\n\s+'[^'\\]*(?:\\.[^'\\]*)*'
This pattern matches:
\s+apiEndpoint:\n\s+ Match 1+ whitespace chars, apiEndpoint: a newline and 1+ whitespace chars
' Match '
[^'\\]* Match 0* times any char except ' or \
(?:\\.[^'\\]*)* Optionally repeat matching \ followed by any char and again 0* times any char except ' or \
' Match '
» Regex demo | Regex graph
as regex you can use:
/apiEndpoint:'https?:\/\/\w+(:[0-9]*)?(\.\w+)?'/gm
to insert the text you can use:
e.g. baseUrl = 'https://localhost:6000'
`${baseUrl}`
Note:
You can check your regex here: https://regex101.com/
e.g.:

Replace all occurrences except for the 3rd

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
--------------------------------------------------------------------------------
, ','

How can I only match a leading space followed by a non numeric in Regex

I need some assistance, I have been at this for hours now. I am not winning.
I need to match a space only if its followed by a non-numeric character (which I will replace with blank to remove it from the string).
I have tried this ^[^\s+]+\D and it works to some extent.
if I have the string " JLABCD-1 836397-BTD56517" it return correctly without the leading space, which is what I want "JLABCD-1 836397-BTD56517"
if I have " BefhMS JLZARL-1 836397-BTD56517" it returns this "JLZARL-1 836397-BTD56517"
But if I don't have a space before the the first word, I want it to ignore all other spaces.
If I have "_JLABCD-1 836397-BTD56517", I want to return "JLABCD-1 836397-BTD56517" or the original string as it is. Not "836397-BTD56517" which is what I am getting at the moment.
Is this possible with Regex?
Use a look ahead:
"^ +(?=\D)"
but it seems you just want to match any leading spaces. If so, just use:
"^ +"
The negated (due to its first character being ^) character class [^\s+] in your regex matches anything not whitespace or a +.
Use
^\s+(\D)
Replace with $1, it is a backreference to the capturing group (\D). Or \1 if $1 does not work.
See proof.
Explanation
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
\s+ whitespace (\n, \r, \t, \f, and " ") (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1:
--------------------------------------------------------------------------------
\D non-digits (all but 0-9)
--------------------------------------------------------------------------------
) end of \1

Need help stopping at parenthesis

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