This is not one of these "help me build my regex" questions. I have an HTML form input field where a user can provide geographical position data in various formats. The following regex works fine in regexr.com as well as in my application. However, I want to use the "pattern" parameter of HTML5 to additionally validate a user's input before submitting it.
((([E|W|N|S](\s)?)?([\-]?[0-1]?[(0-9)]{1,2})[°][ ]?([(0-5)]?[(0-9)]{1})([\.|,][0-9]{1,5})?['][ ]?([0-5]{0,1}[0-9]?(([\.|\,])[0-9]{0,3})?)([\"]|[']{2}){0,1}((\s)?[E|W|N|S])?)|([-]?[1]?[0-9]{1,2}[\.|,][0-9]{1,9}))
The point is that this regex contains a quote character ("). Now, I put this regex in my input like this:
<input type="text" pattern = "regex..."...." />
Browsers do not recognize this regex and don't do any validation at all, so obviously I need to escape that quote. What I tried so far:
PHP's addslashes() function escapes too many characters.
I escaped the quote with a single backslash
That did not change anything. I tested with Chrome, which works fine with simple regular expressions. The one above obviously is a bit too complicated.
I know the regular expression above is not perfect for matching coordinates, however, this is not to be discussed here. I just would like to know how to correctly escape a pattern in HTML5 as Chrome does not do anything with that regex.
Use HTML entities:
instead of ' use '
instead of " use "
If you're creating the regexp using PHP, you can use htmlentities() to encode a string using HTML entities. By default, this will just encode double quotes, but you can use the ENT_QUOTES option to encode both types of quotes.
Related
I'm trying to make a regex expression to detect a URL with a dynamic ending from a message. So for example it would be something like this.
"http://loclhost/something/randomstring example text example text example text"
So the "http://localhost/something/" will always be the same but the random string part wont and I want to grab "http://loclhost/something/randomstring" only...
I've tried doing this expression
"/http://localhost/something/(.*) "
The thing is, it selects the whole text. I've tried looking up online but can't find anything. Would love some help :)
The .* will keep 'eating up' characters. You probably want something like
/http:\/\/localhost\/something\/([^\s]*)/
to make it 'stop' at a white-space character. Or
/http:\/\/localhost\/something\/([a-z0-9]*)/
if you are sure that randomstring only contains alpha-numerical characters.
Example: https://regex101.com/r/U12o53/1
You need to modify the (.*) part of the url so it only contains valid url characters, e.g.
/http:\/\/localhost\/something\/([\d\w\-_]*)/
You can modify it as you need based on the characters that can be in randomstring.
I have a set of strings for which I would like to replace single quotes by double quotes. But, sometimes the single quote to replace is at the end of the line, sometimes the single quote should be replaced since it follow a S for possessive.
Example :
The song 'Miss you' is featured in The Rolling Stones' album 'Voodoo Lounge'
should be
The song "Miss you" is featured in The Rolling Stones' album "Voodoo Lounge"
Thanks your help :)
Regular expressions can only deal with raw text. It can't tell context or grammar. So it is pretty much impossible to build up a regular expression that will correctly identify the occurrences of non-possessive s characters.
However, if you'd like to ignore such cases, and match rest of them, you can use the following regex with lookaround assertions:
(?<!s)'(?!s\b)
Note that this will not match for valid cases like Blurred Lines, Dangerous etc.
Working demo
Example text that I am dealing with. This text is a tooltip that is stored in a database that a user can edit using a WYSIWYG editor.
Example tooltip:
BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah
BlahBlahBlahBlah BlahBlah
BlahBlahBlahBlahBlahBlahBlahBlah
In short I need to replace that href to a javascript function in my app:
BlahBlah
goes to:
BlahBlah
I'm trying to write a RegEx expression to find the href tag, get everything between the quotes. I then need to replace everything between the quotes using the above code and put the URL in the function call.
Here's what I've tried to far but it does not work (I struggle with Regular Expressions)
myTooltipText.replaceAll(".*href\\s*=\\s*\(.*)\"","href=\"javascript:void(0);\" onclick="openOrFocus('[SOME_COMPLETE_URL]', 'windowName')";
I'm using Java and Groovy.
Edit: The URL's are never the same; they change per tooltip as they go to an in depth context help.
You don't need .* before href and you're trying to escape your left parenthesis on your capturing group it looks like. You also missed escaping a quotation inside your replacement text, and left off your ending parenthesis ) at the end of your replaceAll function.
Try the following, this captures between your quotes and references to capture group $1 in the replacement.
myTooltipText.replaceAll("href\\s*=\\s*\"([^\"]*)\"", "href=\"javascript:void(0);\" onclick=\"openOrFocus('$1', 'windowName')\"");
See Working demo
I have a load of links that look like this:
Taboola - Content you may like
I want to delete the entire ICON and ADD_DATE attributes and their values.
I'm using sublime with a regex find/replace but I'm not sure how to write the regex to grab everything in between ICON=" AND "
Any help would be appreciated!
This should work (escaping quotes as necessary):
ICON="[^"]*"
The reason ICON=\"(.*)" won't work is that regex can 'be greedy' in what it takes. This means that if it can match more of the string to satisfy the pattern it will.
You can either specify a non greedy search, such as ICON=".*?" or explicitly declare matches on atoms that are not quotes as in the above answer.
I'm trying to write a regex that will match humanly readable quoted values. As one example, XML attributes. The problem I'm running into is that the data between quoted areas is actually quoted as well if you consider an attribute's ending quote and a subsequent attribute's beginning quote. Here's the expression I have so far:
(?<=\")(?(?!\s+\")[^\"]+)(?=\")
What I tried to express in plain English was: A quote (don't capture it), if not followed by just spaces terminating in another quote, match anything not a quote that is followed by another quote (not capturing the last quote).
and here's my sample data:
<computer name = "printserver" model = "1000ZS" />
The regex produces 3 matches:
printserver
model =
1000ZS
I think that if I could find a way to tell the regex engine to skip every other occurrence I'd have it.
Here's another sample data set, sort of like QML class attributes:
field1: "value1" field2: "value2" field3: "value3"
I can "see" the quoted data, but extracting it via regex is beating me :-)
I'm using the .NET 4.5 System.Text.RegularExpressions framework in my project. I'm not targeting a specific markup like XML, JSON, QML, etc. but am looking for a general purpose regex that would just grab the quoted values similar to how we interpret the data as humans...
Any suggestions? Thanks!
You can always consume the quote in your match:
\"([^\"]+)\"
And extract the part you need from the first capture group.
If it's explicitly a quote preceded by a space, then you can use the part you used, with a little tweak:
\"((?:(?!\s+\")[^\"])+)\"
Of if you just know that the string contains simple patterns like that, maybe something like this:
(?:(?!\s+\")[^\"])+(?=\")