Regex - White Space - css

I have a string coming out as:
<span class="abc"> test </span> styletext
I want to grab just the styletext part which is dynamic and give it some css styles.
Can I use regex to select the whole part after the white space and apply the style? How do I target the part after the white space?

Try this as a regex:
^/span\s*(.*)
You may need to escape the "\" depending on the language you are using.
The text captured in the group will be the style text.
It "translates" as: starting at the beginning of the string (^) match the text "/span", followed by any number of whitespace characters (\s*), followed by any number of any type of character (.*). The parenthesis tells it to capture the last part for later use.

Related

How to remove characters between space and specific character in R

I have a question similar to this one but instead of having two specific characters to look between, I want to get the text between a space and a specific character. In my example, I have this string:
myString <- "This is my string I scraped from the web. I want to remove all instances of a picture. picture-file.jpg. The text continues here. picture-file2.jpg"
but if I were to do something like this: str_remove_all(myString, " .*jpg) I end up with
[1] "This"
I know that what's happening is R is finding the first instance of a space and removing everything between that space and ".jpg" but I want it to be the first space immediately before ".jpg". My final result I hope for looks like this:
[1] "This is my string I scraped from the web. I want to remove all instances of a picture. the text continues here.
NOTE: I know that a solution may arise which does what I want, but ends up putting two periods next to each other. I do not mind a solution like that because later in my analysis I am removing punctuation.
You can use
str_remove_all(myString, "\\S*\\.jpg")
Or, if you also want to remove optional whitespace before the "word":
str_remove_all(myString, "\\s*\\S*\\.jpg")
Details:
\s* - zero or more whitespaces
\S* - zero or more non-whitespaces
\.jpg - .jpg substring.
To make it case insensitive, add (?i) at the pattern part: "(?i)\\s*\\S*\\.jpg".
If you need to make sure there is no word char after jpg, add a word boundary: "(?i)\\s*\\S*\\.jpg\\b"

RegEx Expression to validate TextBox data

The validator should allow only alphabetic characters (a-z and A-Z), dots(.),comma(,), slash(/) and hyphen (-). Please help to to find out one. Or tell me how to create one customized to my specifications.
I have tried [a-zA-Z,-/.] and it works but my requirements only allow for a maximum of 1 of each of the non-letter characters I specified (.,/-).
Try: ^[A-Za-z]*[-a-zA-Z,/.]{1}[A-Za-z]*$
Explanation
^ Anchor to start of string
[A-Za-z]* may be surrounded by multiple letters
[-a-zA-Z,/.]{1} Only one of the enclosed characters
[A-Za-z]* may be surrounded by multiple letters
$ Anchor to end of string

Text box validation using regular expression in asp.net

I have a textbox and have to use the regular expressions in asp.net
My text should not allow the spaces in first and last place.
Output should be:
Valid:
[India Bangalore]
Not valid:
[ India Bangalore ]
i.e : user can enter the spaces in between the words but not in first position and last position.
If you have solution in JavaScript that is also fine.
Trim() should remove any trailing or leading spaces.
Try this please :
^[^\s].*[^\s]$
It simply match input which:
not to start with any white space ^[^\s] followed by any character even white spaces .* and not end with any white space [^\s]$.
Any way calling Trim() method on input string in server-side is much easy .

Regular expression to match maximium of five words

I have a regular expression
^[a-zA-Z+#-.0-9]{1,5}$
which validates that the word contains alpha-numeric characters and few special characters and length should not be more than 5 characters.
How do I make this regular expression to accept a maximum of five words matching the above regular expression.
^[a-zA-Z+#\-.0-9]{1,5}(\s[a-zA-Z+#\-.0-9]{1,5}){0,4}$
Also, you could use for example [ ] instead of \s if you just want to accept space, not tab and newline. And you could write [ ]+ (or \s+) for any number of spaces (or whitespaces), not just one.
Edit: Removed the invalid solution and fixed the bug mentioned by unicornaddict.
I believe this may be what you're looking for. It forces at least one word of your desired pattern, then zero to four of the same, each preceded by one or more white-space characters:
^XX(\s+XX){0,4}$
where XX is your actual one-word regex.
It's separated into two distinct sections so that you're not required to have white-space at the end of the string. If you want to allow for such white-space, simply add \s* at that point. For example, allowing white-space both at start and end would be:
^\s*XX(\s+XX){0,4}\s*$
You regex has a small bug. It matches letters, digits, +, #, period but not hyphen and also all char between # and period. This is because hyphen in a char class when surrounded on both sides acts as a range meta char. To avoid this you'll have to escape the hyphen:
^[a-zA-Z+#\-.0-9]{1,5}$
Or put it at the beg/end of the char class, so that its treated literally:
^[-a-zA-Z+#-.0-9]{1,5}$
^[a-zA-Z+#.0-9-]{1,5}$
Now to match a max of 5 such words you can use:
^(?:[a-zA-Z+#\-.0-9]{1,5}\s+){1,5}$
EDIT: This solution has a severe limitation of matching only those input that end in white space!!! To overcome this limitation you can see the ans by Jakob.

regular expression for no blank space b/w text

Please tell me how to check regular expression for no blank space b/w text.
If you mean "What's the reqular expression to check there is no white space in a string", the following pattern will work:
^[\S]*$
This will find any string that only contains non-white space (spaces, new lines, tabs, etc).
I don't know if it is the regular expression you are looking for but, [:space:] will match any whitespace character, while [:blank:] will match space and tab characters only.
Both are used inside square brackets, e.g. [[:space:]]

Resources