regex match if starts or ends with whitespaces
modify it to the following
(^\s+)|(\s+$)
Based on modified OP, Use this Pattern ^\s*(.*?)\s*$
Demo look at capturing group #1
^ # Start of string/line
\s # <whitespace character>
* # (zero or more)(greedy)
( # Capturing Group (1)
. # Any character except line break
*? # (zero or more)(lazy)
) # End of Capturing Group (1)
\s # <whitespace character>
* # (zero or more)(greedy)
$ # End of string/line
Regex: Specify "space or start of string" and "space or end of string"
You can use any of the following:
\b #A word break and will work for both spaces and end of lines.
(^|\s) #the | means or. () is a capturing group.
/\b(stackoverflow)\b/
Also, if you don't want to include the space in your match, you can use lookbehind/aheads.
(?<=\s|^) #to look behind the match
(stackoverflow) #the string you want. () optional
(?=\s|$) #to look ahead.
RegEx pattern to check for whitespace at the start or end of a string
Using \s
for whitespace caused the problem for me. So, I used [[:blank:]]
instead and this works well:
select data_content as 'leading_or_trailing'
from data_table
where data_content regexp '^[[:blank:]]|[[:blank:]]$';
select data_content as 'leading'
from data_table
where data_content regexp '^[[:blank:]]';
select data_content as 'trailing'
from data_table
where data_content regexp '[[:blank:]]$';
JavaScript Regex - Remove Whitespace from Start and End
\s
means whitespace characters in regex, like <space>, <tab>, etc.^
means the beginning of the string$
means the end of the string|
means OR (match the left side or the right side)+
means 1 or more (based off of the rule on the left)/a regex/g
theg
means "global", aka "match multiple times" since you could need to match at the beginning AND end
So the regex means:
/^\s+|\s+$/g
/ / Wrap the regex (how you do it in JS)
^\s+ Try to match at the beginning one or more whitespace chars
| Or...
\s+$ Try to match whitespace chars at the end
g Match as many times as you can
String.prototype.replace replaces the match(es) found in the regex with the string provided as the 2nd argument, in this case an empty string.
So the process internally is:
- Look for all sections that match the regex (which will be the whitespace at the beginning and the whitespace at the end
- Replace each match with
""
, removing those matches entirely
let hello = " Hello, World! ";
let wsRegex = /^\s+|\s+$/g;
let result = hello.replace(wsRegex, "");
console.log('"' + result + '"');
How to regexp match surrounding whitespace or beginning/end of line
Why your approach does not work
\<
, \>
only match against the beginning (or end, respectively) of a word. That means that they can never match if put adjacent to /
(which is not treated as a word-character) – because e.g. \</
basically says "match the beginning of a word directly followed by something other than a word (a 'slash', in this case)", which is impossible.
What will work
This will match /
surrounded by whitespace (\s
) or beginning/end of line:
egrep '(^|\s)/($|\s)' file
(egrep
implies the -E
option, which turns on processing of extended regular expressions.)
What might also work
The following slightly simpler expression will work if a /
is never adjacent to non-word characters (such as *
, #
, -
, and characters outside the ASCII range); it might be of limited usefulness in OP's case:
grep '\B/\B' file
Regex that matches a string that starts with whitespace and has only digits after
The [ 0-9]{7}
will match 7 digits or spaces in any order and this pattern can return partial matches since it is not anchored at the start/end of the string.
You can use a lookahead restricting the length of the string, and use the sequential subpatterns:
^(?=[\s\d]{7}$)\s*\d*$
See the regex demo
The pattern breakdown:
^
- start of string(?=[\s\d]{7}$)
- the string will be matched only if the whole string consists of whitespaces or/and digits of whole length 7\s*
- 0+ whitespace symbols\d*
- 0+ digits$
- end of string.
Related Topics
Counting Number of Zeros Per Row by Pandas Dataframe
Checking If Particular Value (In Cell) Is Nan in Pandas Dataframe Not Working Using Ix or Iloc
Creating New Dataframes in Loop in Python
How to Execute Local Python Scripts in Jenkins Ui
How to Merge 2 CSV Files Together by Multiple Columns in Python
Python - Regex Match Multiple Patterns in Multiple Lines
Python: How to Turn CSV Data in to Array
How to Find and Replace a Part of a Value in Json File
Splitting a Phone Number into a List of Digits: Python
Find the Index of the First Digit in a String
Pandas Convert from Datetime to Integer Timestamp
Find the Index of a Value in a 2D Array
Masking User Input in Python With Asterisks
Easiest Way to Ignore Blank Lines When Reading a File in Python
How to End Program If Input == "Quit" With Many If Statements