English 中文(简体)
如何用双倍数确定奇1。
原标题:how to identify odd 1 s in a binary number
  • 时间:2010-08-05 11:46:00
  •  标签:
  • regex
  • tcl

如何在双位数中辨别出奇1,我的双位数,即每个奇数为一。

1 1 0 0 1 0 0 0 1 0   1  0 1  0  1   1  1 1  1  1  0  1  1  0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
11010101010101111110110
11101101010101100011011
11111100110101010111101

i want get output what bit is a odd 1 one that one i get using basic regular expression the output each string like this

1 1 1  1  1 1  1  1  1
1 5 9 11 13 15 17 19 23

这样一来,每个双轨迹都会得到产出。

最佳回答

假设每一方位数以大端表示的双位数(点数为零,指的是舱面的最后一个特性),确定在座标中所有单位数(即轨道编号为1、比值编号为3、比值编号为5等)在座标语中是否都是通过测试完成的,如果该方位数与这一正常表述相吻合:

^[01]?(?:1[01])*$

为了理解这一点,首先要简单地假设我们已经知道所有特性都是01。 鉴于这种情况(无视不捕的骗局),我们本可以撰写(扩大的形式):

^ .? (1.)* $
A BB CCCCC D <- the key

该编码是全方位(A>>()的固定配对,即空档或数(/code>)或任何编号<代码>1的附则随附任何内容(>>,其中将<1>>>>>>>>> > > > 编码放在“奇”位置上,或连数位数位数位数位数位数位数(<1>>>>>>>> 代码/代码>。 我仅将这一基本形式转化为其面前更有效率和准确的代表性,对符号的字母顺序加以限制([01]>>,并使用未捕获的括号(:......>,而不是(......))。

如果您认为第1轨为第1轨,则需要作如下修改:

^1?(?:[01]1)*$

对于低端轨道体而言,你需要“逆向”(或使用 ,其中显示反向,说明插图应与对方配对并使用。 低端轨道#0- 首先的“逆境”是:

^(?:[01]1)*[01]?$

低端参照标准第一:

^(?:1[01])*1?$

删除所有这些定期表述之后,最好通过在<条码><>>><>/代码>上填入这些词语。


证明:

foreach s {
    110010001010101111110110
    11010101010101111110110
    11101101010101100011011
    11111100110101010111101
    1110111011111010111010
} {
    set matches [regexp {^[01]?(?:1[01])*$} $s]
    puts "$s [lindex {{doesn t match} matches} $matches]"
}

产生这一产出:

110010001010101111110110 doesn t match
11010101010101111110110 doesn t match
11101101010101100011011 doesn t match
11111100110101010111101 doesn t match
1110111011111010111010 matches
问题回答

我将此问题解释为“如果某一双位数含有奇数1 s”**。 该表将与所有双位数相匹配,其编号为even 1 s:

^0*(?:10*10*)*$

抵消结果,以取得您的预期结果。

** 本文件迟交。 我知道,也许并非非常可能,而是很奇怪。

<>Edit: 对照OP的输入确认如下:

110010001010101111110110 - MATCH (even number of 1 s)
11010101010101111110110  - NO MATCH (odd number of 1 s)
11101101010101100011011  - MATCH (even number of 1 s)
11111100110101010111101  - MATCH (even number of 1 s)

你们都需要看上去的是最后一位数。

如果是1,则按数字计算为奇数。

如果是零,就算了。

如果你想要知道如何发现奇数,那么你就只需要看一下最后一位数是否为奇数。

代表:

1$

或者,如果你必须与整个扼杀一致的话:

^[10]+1$


But you don t need regex for this... in pseudo-code:

if right(string,1) == 1

这种方法优于reg,因为reg不上工作,因此,它必须穿透整个扼杀。

/end功能,只能直接看最后特性,当然更简单、更快。

Ok, tcl syntax for that:

if {[string index $str end] eq 1}


If this isn t what you re asking:

  1. Put more details into the question on exactly what you do want.
  2. Mark accepted answers on some of your previous questions.

穿透镜检查灯。





相关问题
Uncommon regular expressions [closed]

Recently I discovered two amazing regular expression features: ?: and ?!. I was curious of other neat regex features. So maybe you would like to share some tricky regular expressions.

regex to trap img tag, both versions

I need to remove image tags from text, so both versions of the tag: <img src="" ... ></img> <img src="" ... />

C++, Boost regex, replace value function of matched value?

Specifically, I have an array of strings called val, and want to replace all instances of "%{n}%" in the input with val[n]. More generally, I want the replace value to be a function of the match ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

Is it possible to negate a regular expression search?

I m building a lexical analysis engine in c#. For the most part it is done and works quite well. One of the features of my lexer is that it allows any user to input their own regular expressions. This ...

regex for four-digit numbers (or "default")

I need a regex for four-digit numbers separated by comma ("default" can also be a value). Examples: 6755 3452,8767,9865,8766,3454 7678,9876 1234,9867,6876,9865 default Note: "default" ...