This question already has answers here:
Using regex in R to find strings as whole words (but not strings as part of words)
(2 answers)
Closed 4 months ago.
I have a data.frame named all that has a column of factors, these factors include "word","nonword" and some others. My goal is to select only the rows that have the factor value "word".
My solution grep("\bword\b",all[,5]) returns nothing.
How come word boundaries are not recognized?
In R, you need two times \:
grep("\\bword\\b", all[5])
Alternative solutions:
grep("^word$", all[5])
which(all[5] == "word")
Related
This question already has answers here:
Find numbers after specific text in a string with RegEx
(3 answers)
Closed 3 years ago.
I have string like this which looks like a url
mainpath/path2/abc/PI 6/j
From the string I need to get the number along with PI
Main problem is the position of PI part wont be always the same. Sometimes it could be at the end. Sometimes at the middle.
So how can I get that number extracted using regex?
I'm really stucked with this
It's as simple as using the RegEx Tool. A Regular Expression of /PI (\d+) and the Output Method of "Parse" should do the trick.
If you're using Alteryx... suppose your field name is [s] and you're looking for [f] (in your example the value of [f] is "PI")... then you could have a Formula tool that first finds /PI by first creating a new field [tmp] as:
SubString([s],FindString([s],"/"+[f])+1)
and then creating the field you're after [target]:
SubString([tmp],0,FindString([tmp],"/"))
From there run [target] through a "Text to Columns" tool to split on the space, which will give you "PI" and "6".
This question already has an answer here:
Notepad++ : Insert blank new line after match of some string
(1 answer)
Closed 4 years ago.
I have to join lines to make questions (having serial no. in order 1,2,3....) in one line and all options as shown below in another single line ? Example :
1.For his alleged
involvements in
espio-nage
(1) abc
(2) saf
(3) asf
(4) aqg
Output should be:
1. For his alleged involvement in espio-nage...
(1)abc (2)saf (3)asf (4)aqg
Note:I just want to make it compact to read in kindle reader so that maximum space will get utilised
I think
\r?\n(?!\(1|\d)
(And replace with empty string) will do what you want
Explanation: Looks for a newline that's not followed by either (1 or a single digit.
If you ever have more than 9 options that will cause problems as it'll split before (10 (11 etc. - in which case \r?\n(?!\(1\)|\d) should do it
Select text/elements and press CTRL+J
This question already has answers here:
Regular expression to match A, AB, ABC, but not AC. ("starts with")
(4 answers)
Closed 4 years ago.
I have an enumeration that I use as fixed parameter values to a program and I use regex to sanitize the user input.
I want the user to be able to enter a partial match to one of the values and accept that value and not other values.
For example if the enumeration is:
end
end now
start
swarm
condition
and the user entered
s
st
sta
etc...
it will be ok because it is part of start;
but if the user entered
ending
it will not be ok because its not part of any of the other words.
I know I can specify each permutation in a group (s|st|sta|star|start) and it will do the work, but doing this for around 12 different values seems very hard to maintain and "ugly'...
Is there an easier way to match a fixed values or sub of those fixed values?
I'm not searching for something that is specific to one engine/language (for example java code..)
Regex is not the correct tool for this job.
Just find the length of the user's input (call it N), then loop through your valid values and see if the first N characters of that value matches the input.
If only one item matches, you've got yourself a result! If more than one matches, you'll need more letters from the user to identify the correct one. And if none match, it's invalid.
This question already has answers here:
Regular expression to count number of commas in a string
(9 answers)
Closed 4 years ago.
I need to check for users that are putting their id in the wrong input box.
They might enter the id as 123-456-789-012 or 123456789012 or some variation so I can't just check for digits. The length of the id varies slightly per user, but is always more than 10 digits.
Valid input is a mix of characters and 0-10 digits.
I've seen a lot of solutions for plain digits, but not mixed text. I tried variations of
(\D*\d){0,10}
but that didn't work.
You want "0-n non-digits" then "1-10 lots of a digit-and-any-non-digits":
^\D*(\d\D*){1,10}$
This question already has an answer here:
Increasing the print depth in SML/NJ
(1 answer)
Closed 6 years ago.
I'm using Emacs to write a simple function in sml of about 4 lines and when I try to call the function/evaluate it in the buffer it returns this with 3 dots at the end
val it = [1,2,2,2,2,2,2,2,2,2,2,2,...] : int list
What's the dots at the end? My code doesn't print any dots. Is this is from Emacs or sml? any hints please.
Also I'm supposed to get a longer list like
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]
is that why the dots there?
is that why the dots there?
Yes.