I have a line of code:
mainURL=Sample%20Line%20Of%20Code.php
I want to replace the %20 with a dash (-) using Dreamweaver's find and replace
I have tried several variations of:
Find - mainURL=([^\s]*).phpand mainURL=([^%20]*).php
Replace - mainURL=([^-]*).phpit seems to find my string but literally replaces it with mainURL=([^-]*).phpI'm fairly new to regular expressions and could use some help.
Thanks in advance - Tom
You may use
Find What: (\G(?!\A)|mainURL=(?=\S*\.php))((?:(?!\.php)\S)*?)%20
Replace with: $1$2-
See the regex demo.
Details
(\G(?!\A)|mainURL=(?=\S*\.php)) - Group 1 capturing mainURL= that is followed with 0+
non-whitespace and .php substring or the end of the previous match
((?:(?!\.php)\S)*?) - Group 2: any non-whitespace char that is not a starting point for .php substring, 0+ repetitions, as few as possible
%20 - a literal substring.
Related
I'm using regex word boundary \b, and I'm trying to match a word in the following sentence but the result is not what I need. Connector Punctuations (such as underscore) are not being considered as a word boundary
Sentence: ab﹎cd_de_gf|ij|kl|mn|op_
Regexp: \\bkl\\b
However, de is not getting matched.
I tried updating the regexp to use unicode connector punctuation (it's product requirement as we support CJK languages as well) but that isn't working.
Regexp: (?<=\\b|[\p{Pc}])de(?=\\b|[\p{Pc}])
What am i missing here?
Note: (?<=\\b|_)de(?=\\b|_) seems to work for underscores but i need the regex to work for all the connector punctuations.
Thanks in advance !!
Based on the use case you have described you can simplify your regex to:
(?<![[:alnum:]])de(?![[:alnum:]])
instead of trying to match word boundaries, unicode punctuation characters etc.
This will match de if it not followed or preceded by any alpha-numeric character.
To match any connector punctuation characters you need \p{Pc}:
(?<=\\b|\\p{Pc})de(?=\\b|\\p{Pc})
NOTE: \p{Pc} can also be written as [_\u203F\u2040\u2054\uFE33\uFE34\uFE4D-\uFE4F\uFF3F] that matches all these 10 chars.
$.validator.addMethod('AZ09_', function (value) {
return /^[a-zA-Z0-9.-_]+$/.test(value);
}, 'Only letters, numbers, and _-. are allowed');
When I use somehting like test-123 it still triggers as if the hyphen is invalid. I tried \- and --
Escaping using \- should be fine, but you can also try putting it at the beginning or the end of the character class. This should work for you:
/^[a-zA-Z0-9._-]+$/
Escaping the hyphen using \- is the correct way.
I have verified that the expression /^[a-zA-Z0-9.\-_]+$/ does allow hyphens. You can also use the \w class to shorten it to /^[\w.\-]+$/.
(Putting the hyphen last in the expression actually causes it to not require escaping, as it then can't be part of a range, however you might still want to get into the habit of always escaping it.)
The \- maybe wasn't working because you passed the whole stuff from the server with a string. If that's the case, you should at first escape the \ so the server side program can handle it too.
In a server side string: \\-
On the client side: \-
In regex (covers): -
Or you can simply put at the and of the [] brackets.
Generally with hyphen (-) character in regex, its important to note the difference between escaping (\-) and not escaping (-) the hyphen because hyphen apart from being a character themselves are parsed to specify range in regex.
In the first case, with escaped hyphen (\-), regex will only match the hyphen as in example /^[+\-.]+$/
In the second case, not escaping for example /^[+-.]+$/ here since the hyphen is between plus and dot so it will match all characters with ASCII values between 43 (for plus) and 46 (for dot), so will include comma (ASCII value of 44) as a side-effect.
\- should work to escape the - in the character range. Can you quote what you tested when it didn't seem to? Because it seems to work: http://jsbin.com/odita3
A more generic way of matching hyphens is by using the character class for hyphens and dashes ("\p{Pd}" without quotes). If you are dealing with text from various cultures and sources, you might find that there are more types of hyphens out there, not just one character. You can add that inside the [] expression
I have an HTML5 input element with a pattern attribute. I'm having some trouble with an optional group.
The (relative) URL must start with a forward slash (I have this working).
The total (relative) URL may contain a total of up to 255 characters.
All characters from 2-255 must be (lowercase) alpha-numeric or a forward slash.
Separately the forward slash regex works and the 2-255 part works for alpha-numeric and forward slashes. However I'm having trouble allowing both groups with the second group being optional.
What I have confirmed to work:
pattern="^\/"
pattern="[a-z0-9\/]"
However I can't determine how to allow the second group as an option (I've tried adding the ? after the ending square bracket in example without luck).
I also am not sure how to combine the length ({255,}) bit to the total pattern expression.
How do I combine all three aspects of the regular expression?
Note: tags seem to be broken at the moment of posting this.
You can use
pattern="/[a-z0-9/]{0,254}"
You do not need ^ nor $ in the pattern regex, by the way, it must match the whole string anyway, it will be parsed as ^(?:/[a-z0-9/]{0,254})$ pattern. That is, it will match a string that starts with / and then contains 0 to 254 lowercase ASCII letters, digits or slashes till the string end.
Note that / should only be escaped in regex literals where / is used as a delimiter char. pattern regexps are defined with literal strings.
I am trying to extract an email address from user input text in Watson Conversation. First thing first, I need to trigger a particular node using an if condition like this:
input.text.contains('\^(([^<>()[].,;:s#\"]+(.[^<>()[].,;:s#\"]+)*)|(\".+\"))#(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$\')
But it doesn't work, I tried a lot of regexes that I found on the internet but none of them work. Does anyone know how to write a proper regex?
I suggest using a much simpler, approximate, regex to match emails that you need to use with String.matches(string regexp) method that accepts a regex:
input.text.matches('^\\S+#\\S+\\.\\S+$')
Do not forget to double escape backslashes so as to define literal backslashes in the pattern.
Pattern details:
^ - start of string
\\S+ - one or more non-whitespace chars
# - a # symbol
\\S+ - one or more non-whitespace chars
\\. - a literal dot
\\S+ - one or more non-whitespace chars
$ - end of string.
I have output from grep I'm trying to clean up that looks like:
<words>Http://www.path.com/words</words>
I've tried using...
sed 's/<.*>//'
...to remove the tags, but that just destroys the entire line. I'm not sure why that's happening, since every '<' is closed with a '>' before it gets to the content.
What is the easiest way to do this?
Thanks!
Try this for your sed expression:
sed 's/<.*>\(.*\)<\/.*>/\1/'
Quick breakdown of the expression:
<.*> - Match the first tag
\(.*\) - Match and save the text between the tags
<\/.*> - Match the end tag making sure to escape the / character
\1 - Output the result of the first saved match
- (the text that is matched between \( and \))
More about back-references
A question came up in the comments that should probably be addressed for completeness.
The \( and \) are Sed's back-reference markers. They save a portion of the matched expression for use later.
For example, if we have an input string:
This has (parens) in it. In addition we can use parenslike thisparens
using back-references.
We develop an expression:
sed s/.*(\(.*\)).*\1\\(.*\)\1.*/\1 \2/
Which gives us:
parens like this
How the heck did that work? Let's break down the expression to find out.
Expression breakdown:
sed s/ - This is the opening tag to a sed expression.
.* - Match any character to start (as well as nothing).
( - Match a literal left parenthesis character.
\(.*\) - Match any character and save as a back-reference. In this case it will match anything between the first open and last close parenthesis in the expression.
) - Match a literal right parenthesis character.
.* - Same as above.
\1 - Match the first saved back-reference. In the case of our sample this is filled in with `parens`
\(.*\) - Same as above.
\1 - Same as above.
/ - End of the match expression. Signals transition to the output expression.
\1 \2 - Print our two back-references.
/ - End of output expression.
As we can see, the back-reference taken from between the parenthesis (( and )) was substituted back into the matching expression to be able to match the string parens.