Java 8 LocalDate:确定本月的哪个星期一(第一,第二,第三等)(Java 8 LocalDate: Determine What Monday of the Month its (1st, 2nd, 3rd, etc))
我试图弄清楚如何确定一个特定的星期一是特定月份的第一,第二,第三还是第四个星期一。 我已经想出了如何获得使用LocalDate类的月份的下个星期一和星期。
LocalDate now = LocalDate.of(2018, 2, 1); LocalDate nextMonday = now.with(next(DayOfWeek.MONDAY)); WeekFields weekFields = WeekFields.of(Locale.getDefault()); int week = nextMonday.get(weekFields.weekOfMonth());
在上面的例子中,代码得到下一个星期一以及它所在的星期。星期是2月的第二周,但是星期一不是第二个星期一,它是第一个。 任何帮助,这将不胜感激。
I'm trying to figure out how to determine whether a particular Monday is the first, second, third or fourth Monday of a given month. I've figured out how to get the next Monday and the Week of the of month that resides in using the LocalDate class.
LocalDate now = LocalDate.of(2018, 2, 1); LocalDate nextMonday = now.with(next(DayOfWeek.MONDAY)); WeekFields weekFields = WeekFields.of(Locale.getDefault()); int week = nextMonday.get(weekFields.weekOfMonth());
Case in the example above, the code gets the next Monday and the week it resides in. Week is the second week of Feb, but that Monday is not the second Monday, it's the first. Any help with this would be greatly appreciated.
原文:https://stackoverflow.com/questions/48155322
最满意答案
就像已经在这里回答的,最简单的方法就是删除
+
:^123\\d{9}$
要么
^123\\d{6}$
取决于你确切需要什么。
你也可以使用另一种更复杂一般的方法,一种负面的看法:
(?!.{10,})^123\\d+$
说明:
这个:
(?!.{10,})
是一个负向预读 (?=
会是一个正向预读),这意味着如果预读之后的表达式匹配这个模式,那么整个字符串不会' t匹配。 粗略地说,这意味着: 只有在负面预测中的模式不匹配时,才能满足此正则表达式的标准 。在这种情况下, 只有在
.{10}
不匹配的情况下字符串才匹配,这意味着10个或更多字符,因此只有当前面的模式匹配最多9个字符时才匹配。积极的预见反其道而行之,只有在先行标准也匹配的情况下才能进行匹配。
只是为了好奇而把它放在这里,它比你需要的更复杂。
Like already answered here, the simplest way is just removing the
+
:^123\\d{9}$
or
^123\\d{6}$
Depending on what you need exactly.
You can also use another, a bit more complicated and generic approach, a negative lookahead:
(?!.{10,})^123\\d+$
Explanation:
This:
(?!.{10,})
is a negative look-ahead (?=
would be a positive look-ahead), it means that if the expression after the look-ahead matches this pattern, then the overall string doesn't match. Roughly it means: The criteria for this regular expression is only met if the pattern in the negative look-ahead doesn't match.In this case, the string matches only if
.{10}
doesn't match, which means 10 or more characters, so it only matches if the pattern in front matches up to 9 characters.A positive look-ahead does the opposite, only matching if the criteria in the look-ahead also matches.
Just putting this here for curiosity sake, it's more complex than what you need for this.
相关问答
更多-
您可以预见,检查您在接下来的5个字符范围内没有填充字符和数字的倒序序列,然后需要并捕获5个仅为数字和填充字符的字符: ABC(?!.{0,3}\d\$)([\$\d]{5})\$!@ 如果您至少需要一位数字,那么: ABC(?!.{0,3}\d\$)([\$\d]{4}\d)\$!@ You could look ahead to check that you don't have an inverted sequence of padding character and digit within the ...
-
您需要将两者结合起来并使用{11}来匹配完全匹配的11个字符。 /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{11}$/ 地点: (?=.*\d)断言位置跟随任何位置的数字(其中\d等于[0-9] )。 (?=.*[a-zA-Z])断言位置在任何位置都跟着字母表。 [a-zA-Z0-9]{11}仅在长度为11个字符且在字符类中时才匹配。 ^和$是开始和结束锚点,有助于检查整个字符串。 You need to combine both and use {11} for exact ...
-
你可以用 ^(01\d{14})(17\d{6})?(10\w{1,20})?(21\w{1,20})?$ 请参阅正则表达式演示 请注意,如果您计划对捕获组进行量化,则不必将捕获组包装为非捕获组,您可以直接量化捕获组。 另外,要使一个组可选,它就足够了? 量词,因为*匹配0次或更多次出现。 图案细节 ^ - 字符串的开头 (01\d{14}) - 组01和24位 (17\d{6})? - 第2组(可选): 17和6位数 (10\w{1,20})? - 第3组(可选): 10和1到20个字符 (21\w{1 ...
-
就像已经在这里回答的,最简单的方法就是删除+ : ^123\\d{9}$ 要么 ^123\\d{6}$ 取决于你确切需要什么。 你也可以使用另一种更复杂一般的方法,一种负面的看法: (?!.{10,})^123\\d+$ 说明: 这个: (?!.{10,})是一个负向预读 ( ?=会是一个正向预读),这意味着如果预读之后的表达式匹配这个模式,那么整个字符串不会' t匹配。 粗略地说,这意味着: 只有在负面预测中的模式不匹配时,才能满足此正则表达式的标准 。 在这种情况下, 只有在.{10}不匹配的情况 ...
-
尝试以下操作: ^([0-9]{4})?$ ^ - 行首 ([0-9]{4})? - 四个数字,可选(因为? ) $ - 行尾 Try the following: ^([0-9]{4})?$ ^ - start of line ([0-9]{4})? - four numbers, optionally (because of the ?) $ - end of line
-
如何使用正则表达式匹配以数字结尾的固定字符串[duplicate](How to match a fixed string ending with a number using regex [duplicate])[2022-06-21]
你只需要你的常数后跟至少一位数( \d+ ): private static Pattern SPARTA_REGEX = Pattern.compile("This is Sparta(\\d)+"); You just need your constant followed by at least one digit (\d+): private static Pattern SPARTA_REGEX = Pattern.compile("This is Sparta(\\d)+"); -
这将多次处理多个空格 (([A-Z]\s*){3}([0-9]\s*){10}) 由于AD E12 34567890符合该模式,因此将与AB CAAAA AD E12 34567890 AD E12 34567890匹配 https://regex101.com/r/bK3sF8/1 this will handle multiple spaces multiple times (([A-Z]\s*){3}([0-9]\s*){10}) will match AB C A A A A AD E12 34 ...
-
java正则表达式拆分字符串(java regex split strings)[2022-10-22]
我建议这个解决方案 Matcher m = Pattern.compile("\\$\\{(.+?)\\}").matcher(test); while(m.find()) { System.out.println(m.group(1)); } I suggest this solution Matcher m = Pattern.compile("\\$\\{(.+?)\\}").matcher(test); while(m.find()) { ... -
您可以使用此基于前瞻性的正则表达式使用捕获的组获取所有3位数字: (?=(\d{3})) Lookahead是零宽度断言,使我们能够在不实际移动内部正则表达式指针的情况下预测3位数字。 在java中使用: "(?=(\\d{3}))" RegEx演示 You can use this lookahead based regex to grab all the 3 digit numbers using captured groups: (?=(\d{3})) Lookahead is zero-wid ...
-
正则表达式的固定长度字符串,只允许在最后的空间。(Regex for fix length string which allow space(s) at end only. C#)[2022-01-23]
您可以使用 ^(?=.{20}$)[a-zA-Z0-9]*\s*$ 请参阅正则表达式演示 在此表达式中,使用正向前瞻(?=.{20}$)强制执行20个字符的长度。 由于匹配模式只匹配AZ,az和0-9,所以. 在前瞻中是可能的。 正则表达式解释 : ^ - 字符串的开头 (?=.{20}$) - 该字符串的长度必须为20个字符 [a-zA-Z0-9]* - 零个或多个字母或数字(a +可代替* ) \s* - 零个或多个空格 $ - 字符串的结尾 C#: var reg = @"^(?=.{20}$)[a ...