I have the follow wing text on my html pages and I need to replace some words. For example:
"./images/delete.html"
"http.google.com"
this is a text
I have to retrieve the string with the following criteria:
words enclosed in a ""
words with ./images/ in any position (or after opening ")
words ending with .html
In my example, only the "./images/delete.html" should be returned.
Can someone please help me. Thanks!
You could try the below regex whch uses a negated character class [^"] matches any char but not of " zero or more times.
"[^"]*\.\/images\/[^"]*\.html"
DEMO
Related
i need help with vim and regex.
i have a html file with a lot of class="..."
e.g.
<td class="td_3"><p class="block_3">80 €</p></td>
<td class="td_3"><p class="block_3">90 €</p></td>
Since i'm not using any css, i want to delete them.
i tried:
:%s/class="[a-z0-9]"//g
but it's not working.
What am i doing wrong?
With class="[a-z0-9]" pattern, you match a single alphanumeric char in between quotes, while there may be any text other than double quotation mark.
You probably also want to remove the whitespaces before the class.
You may use
:%s/\s\+class="[^"]*"//g
Here, \s\+ will match one or more whitespace chars, class=" matches a literal string, then [^"]* finds any zero or more chars other than " as many as possible and then a " matches the closing double quotation mark.
Recently I tried alot but I still unable to figure how should I able to validation for my text field. I hope I can get some help from here, my questions is I want to validate input text field only accept A-Z a-z 0-9 and space within word. And at least one char or number.
For example, "abc def" , "abc092", "abcdef"
Only in HTML input tag element .
I tried this
but this pattern unable to fullfil my requirements.
the pattern i want to achieve is
1) abc def
2) abcdef
3) abc123
4) a1b2c3 d4e5
5) allow to have empty space within words
the pattern i dont want to accept is
1) empty string
2) no alot of whitespace at the begining or end of the string
3) no bracket and etc special characters
Try
<input type="text" pattern="^\w+([\w ]*\w)*$">
Basically the break down is this:
\w+ - Select a word character ("A-z0-9") one or more times
()* - Select what's in here 0 or more times, which is
[\w ]*\w - Select a word character or space one or more times followed by another word character
No leading or trailing white space allowed. Only word characters allowed and internal spaces.
For some unit tests and breakdown of the regex see: https://regex101.com/r/7UnL9J/1
You can use the Pattern attribute with regex but it is supported only in HTML 5.
Like this " id="username" pattern="[A-Za-z0-9]+"
Check the below link for more information
https://html.com/attributes/input-pattern/#Username_Patterns
Have you tried this?
<input type="text" pattern="[a-zA-Z0-9\s]+">
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
How do I match and replace text using regular expressions in multiline mode?
I know the RegexOptions.Multiline option, but what is the best way to specify match all with the new line characters in C#?
Input:
<tag name="abc">this
is
a
text</tag>
Output:
[tag name="abc"]this
is
a
test
[/tag]
Aahh, I found the actual problem. '&' and ';' in Regex are matching text in a single line, while the same need to be escaped in the Regex to work in cases where there are new lines also.
If you mean there has to be a newline character for the expression to match, then \n will do that for you.
Otherwise, I think you might have misunderstood the Multiline/Singleline flags. If you want your expression to match across several lines, you actually want to use RegexOptions.Singleline. What it means is that it treats the entire input string as a single line, thus ignoring newlines. Is this what you're after...?
Example
Regex rx = new Regex("<tag name=\"(.*?)\">(.*?)</tag>", RegexOptions.Singleline);
String output = rx.Replace("Text <tag name=\"abc\">test\nwith\nnewline</tag> more text...", "[tag name=\"$1\"]$2[/tag]");
Here's a regex to match. It requires the RegexOptions.Singleline option, which makes the . match newlines.
<(\w+) name="([^"]*)">(.*?)</\1>
After this regex, the first group contains the tag, the second the tag name, and the third the content between the tags. So replacement string could look like this:
[$1 name="$2"]$3[/$1]
In C#, this looks like:
newString = Regex.Replace(oldString,
#"<(\w+) name=""([^""]*)"">(.*?)</\1>",
"[$1 name=\"$2\"]$3[/$1]",
RegexOptions.Singleline);
I'm trying to filter using regex in mySQL.
The field is a text field and I want to find all that match 'MD' or similar ('M.D.', 'M. D.', 'DDS, M.D.' etc.).
I do not want to accept those that contain M and D as a part of another acronym (e.g., 'DMD'). However 'DMD, M.D.' I would want to find.
Apologies if this is a simple task - I read through some regex tutorials and couldn't figure this out! Thanks.
Update:
With help from the suggestions I arrived at the following solution:
(\s|^)M\.?\s*D\.?
which works for all of my cases. The quotes in my questions were to indicate it was a string, they are not a part of the string.
You can use a regex like this:
\b(M\.?\s*D\.?|D\.?\s*D\.?\s*S\.?)
Working demo
If I have understood your requirement:
'([^'.]*[ ,]*M[. ]*D[. ]*)'
this looks for MD preceded by space comma or ' separated by 0 or more dots & spaces, followed by '
it matches all the contents between the '' marks
test: https://regex101.com/r/oV2kV8/2
In the end I found this solution works:
(\s|^)M\.?\s*D\.?(\s|$)
This allows for the 'MD' to be at the start or after another credential and to have spaces or periods or nothing between the letters.
I'm using Sublime Text and I need to come up with a regex that will find the whitespaces between a certain opening and closing tag and replace them with commas.
Example: Replace white space in
<tags>This is an example</tags>
so it becomes
<tags>This,is,an,example</tags>
Thanks!
You have just to use a simple regex like:
\s+
And replace it with with a comma.
Working demo
This will find instances of
<tags>...</tags>
with whitespace between the tags
(<tags>\S+)\W(.+</tags>)
This will replace the first whitespace with a comma
\1,\2
Open Find and Replace [OS X Cmd+Opt+F :: Windows Ctrl+H]
Use the two values above to find and replace and use the 'Replace All' option. Repeat until all the whitespaces are converted to commas.
The best answer is probably a quick script but this will get you there fairly fast without needing to do any coding.
You can replace any one or more whitespace chunks in between two tags using a single regular expression:
(?s)(?:\G(?!\A)|<tags>(?=.*?</tags>))(?:(?!</?tags>).)*?\K\s+
See the regex demo. Details
(?s) - a DOTALL inline modifier, makes . match line breaks
(?:\G(?!\A)|<tags>(?=.*?</tags>)) - either the end of the previous successful match (\G(?!\A)) or (|) <tags> substring that is immediately followed with any zero or more chars, as few as possible and then </tags> (see (?=.*?</tags>))
(?:(?!</?tags>).)*? - any char that does not start a <tags> or </tags> substrings, zero or more occurrences but as few as possible
\K - match reset operator
\s+ - one or more whitespaces (NOTE: use \s if each whitespace must be replaced).
SublimeText settings: